Class TokenBucket<T>
- Namespace
- Sportradar.LiveData.Sdk.Services.RateLimiter
- Assembly
- Sportradar.LiveData.Sdk.dll
A generic implementation of a token-bucket (used for traffic shaping purposes).
Simple analogy: there is bucket with a certain amount of tokens. If there are enough tokens inside, an element of type T can be forwarded. Every second another n tokens are added to the bucket and thus making it possible to forward more. The bucket can hold a maximum amount of tokens, no more than that can be added. If the bucket is relatively big, bursts are allowed.
The preferred way of using this class is constructing a token-bucket that corresponds to the desired link speed, calling Dispatch() for each PDU and setting the forward_callback so something will happen with the PDU at the correct interval.
If a simulation of a link speed is undesirable, you might also use the FillBucketByHand() method when a PDU was actually sent.
Dispatch(e) -> TOKENBUCKET -> forward_callback(e)
public class TokenBucket<T>
Type Parameters
TType of the elements in the token-bucket
- Inheritance
-
TokenBucket<T>
- Inherited Members
Constructors
TokenBucket(string, int, int, int, int, ObjToSizeDelegate, ForwardDelegate)
Construct a new token-bucket.
public TokenBucket(string name, int bucket_capacity, int initial_bucket_size, int bucket_fill_speed, int queue_size, TokenBucket<T>.ObjToSizeDelegate size_callback, TokenBucket<T>.ForwardDelegate forward_callback)
Parameters
namestringbucket_capacityintMaximum size of the token-bucket in bytes. If higher than link speed certain traffic bursts can be handled.
initial_bucket_sizeintInitial size of the token-bucket in bytes. If higher than link speed certain traffic bursts can be handled (if the channel is idle long enough).
bucket_fill_speedintBucket fill speed in bytes per second.
queue_sizeintThe internal "overflow" queue size. When this parameter is:
0: Dispatch() can be called that many times without blocking = 0: Dispatch() will always block < 0: Dispatch() will never block (but the queue will get filled more and more)
size_callbackTokenBucket<T>.ObjToSizeDelegateforward_callbackTokenBucket<T>.ForwardDelegate
Fields
DEF_QUEUE_SIZE
public const int DEF_QUEUE_SIZE = 512
Field Value
Properties
Name
Name of this token bucket instance
public string Name { get; }
Property Value
Methods
Dispatch(T)
Try to dispatch a new element.
public void Dispatch(T obj)
Parameters
objTAn element to be dispatched (usually a PDU)
Remarks
Whether this method blocks or not depends on the queue_size parameter used when constructing the TokenBucket.
FillBucket(int)
Mark that another amount of bytes was sent. Adds tokens to the token-bucket.
public int FillBucket(int bytes)
Parameters
bytesintThe number of bytes
Returns
- int
The new number of available bytes
FillBucket(T)
Mark that another element obj was sent and that the corresponding amount of bytes are available again. Adds tokens to the token-bucket.
public int FillBucket(T obj)
Parameters
objTThe element
Returns
- int
The new number of available bytes
StartFilling()
Start with the filling of the token-bucket through time.
public void StartFilling()