Class TokenBucket
This class is responsible for managing adaptive retry mode client rate limiting.
public class TokenBucket
- Inheritance
-
TokenBucket
- Inherited Members
Constructors
TokenBucket()
public TokenBucket()
TokenBucket(double, double, double, double, double)
public TokenBucket(double minFillRate, double minCapacity, double beta, double scaleConstant, double smooth)
Parameters
Properties
CurrentCapacity
The current capacity of the token
protected double CurrentCapacity { get; set; }
Property Value
Enabled
Boolean indicating if the token bucket is enabled.
protected bool Enabled { get; set; }
Property Value
FillRate
The rate at which token are replenished.
protected double? FillRate { get; set; }
Property Value
LastMaxRate
The maximum rate when the client was last throttled.
protected double LastMaxRate { get; set; }
Property Value
LastThrottleTime
The last time when the client was throttled.
protected double LastThrottleTime { get; set; }
Property Value
LastTimestamp
The last time the token bucket was refilled.
protected double? LastTimestamp { get; set; }
Property Value
LastTxRateBucket
The last half second time bucket used.
protected double LastTxRateBucket { get; set; }
Property Value
MaxCapacity
The maximum capacity allowed in the token
protected double? MaxCapacity { get; set; }
Property Value
MeasuredTxRate
The smoothed rate which tokens are being retrieved.
protected double MeasuredTxRate { get; set; }
Property Value
RequestCount
The number of requests seen within the current time
protected long RequestCount { get; set; }
Property Value
TimeWindow
The cached time window calculation
protected double TimeWindow { get; set; }
Property Value
Methods
CUBICSuccess(double)
Calculates the rate
protected virtual double CUBICSuccess(double timestamp)
Parameters
timestamp
double
Returns
- double
Returns the calculated rate for a successful call
CUBICThrottle(double)
Calculates the rate.
protected virtual double CUBICThrottle(double rateToUse)
Parameters
rateToUse
doubleThe rate to use in the calculation
Returns
- double
Returns the calculated rate for a throttled call
CalculateTimeWindow()
protected virtual void CalculateTimeWindow()
CalculateWait(double, double, double)
protected virtual int CalculateWait(double amount, double currentCapacity, double fillRate)
Parameters
Returns
GetTimestamp()
protected virtual double GetTimestamp()
Returns
TokenBucketRefill()
protected virtual void TokenBucketRefill()
TokenBucketUpdateRate(double)
protected virtual void TokenBucketUpdateRate(double newRps)
Parameters
newRps
double
TryAcquireToken(double, bool)
This method attempts to acquire capacity from the client's token
public bool TryAcquireToken(double amount, bool failFast)
Parameters
amount
doubleThe amount of capacity to obtain from the token bucket
failFast
boolIndicates that the client should or shouldn't sleep and try again if capacity cannot be obtained.
Returns
TryAcquireTokenAsync(double, bool, CancellationToken)
This method attempts to acquire capacity from the client's token
public Task<bool> TryAcquireTokenAsync(double amount, bool failFast, CancellationToken cancellationToken)
Parameters
amount
doubleThe amount of capacity to obtain from the token bucket
failFast
boolIndicates that the client should or shouldn't sleep and try again if capacity cannot be obtained.
cancellationToken
CancellationTokenToken which can be used to cancel the task.
Returns
UpdateClientSendingRate(bool)
Updates the sending rate within the client's token bucket
public void UpdateClientSendingRate(bool isThrottlingError)
Parameters
isThrottlingError
boolIndicates if the request resulted in a throttling error.
UpdateMeasuredRate()
protected virtual void UpdateMeasuredRate()
WaitForToken(int)
protected virtual void WaitForToken(int delayMs)
Parameters
delayMs
int
WaitForTokenAsync(int, CancellationToken)
protected virtual Task WaitForTokenAsync(int delayMs, CancellationToken cancellationToken)
Parameters
delayMs
intcancellationToken
CancellationToken