Class TokenBucket

Namespace
Unity.Multiplayer.Core

A token bucket: holds up to Capacity tokens, refills at RatePerSecond, and each accepted call takes one. Starts full, so a sender may burst Capacity calls and then RatePerSecond a second after that.

public sealed class TokenBucket
Inheritance
TokenBucket
Inherited Members

Remarks

Engine-free: time is passed in, in seconds, on any clock that never runs backwards.

Constructors

TokenBucket(double, int, double)

A full bucket of capacity tokens refilled at ratePerSecond, as of now.

public TokenBucket(double ratePerSecond, int capacity, double now)

Parameters

ratePerSecond double
capacity int
now double

Exceptions

ArgumentOutOfRangeException

The rate is not positive (or is NaN or infinite) or the capacity is below one.

Properties

Capacity

The most tokens the bucket holds.

public int Capacity { get; }

Property Value

int

RatePerSecond

Tokens added per second.

public double RatePerSecond { get; }

Property Value

double

Tokens

Tokens available as of the last call (refilled lazily by TryTake(double) and Available(double)).

public double Tokens { get; }

Property Value

double

Methods

Available(double)

The tokens available at now.

public double Available(double now)

Parameters

now double

Returns

double

TryTake(double)

Takes one token if there is one at now.

public bool TryTake(double now)

Parameters

now double

Returns

bool

True when the call is allowed.