Table of Contents

Class TokenBucket

Namespace
Amazon.Runtime.Internal
Assembly
AWSSDK.Core.dll

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

minFillRate double
minCapacity double
beta double
scaleConstant double
smooth double

Properties

CurrentCapacity

The current capacity of the token

protected double CurrentCapacity { get; set; }

Property Value

double

Enabled

Boolean indicating if the token bucket is enabled.

protected bool Enabled { get; set; }

Property Value

bool

FillRate

The rate at which token are replenished.

protected double? FillRate { get; set; }

Property Value

double?

LastMaxRate

The maximum rate when the client was last throttled.

protected double LastMaxRate { get; set; }

Property Value

double

LastThrottleTime

The last time when the client was throttled.

protected double LastThrottleTime { get; set; }

Property Value

double

LastTimestamp

The last time the token bucket was refilled.

protected double? LastTimestamp { get; set; }

Property Value

double?

LastTxRateBucket

The last half second time bucket used.

protected double LastTxRateBucket { get; set; }

Property Value

double

MaxCapacity

The maximum capacity allowed in the token

protected double? MaxCapacity { get; set; }

Property Value

double?

MeasuredTxRate

The smoothed rate which tokens are being retrieved.

protected double MeasuredTxRate { get; set; }

Property Value

double

RequestCount

The number of requests seen within the current time

protected long RequestCount { get; set; }

Property Value

long

TimeWindow

The cached time window calculation

protected double TimeWindow { get; set; }

Property Value

double

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 double

The 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

amount double
currentCapacity double
fillRate double

Returns

int

GetTimestamp()

protected virtual double GetTimestamp()

Returns

double

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 double

The amount of capacity to obtain from the token bucket

failFast bool

Indicates that the client should or shouldn't sleep and try again if capacity cannot be obtained.

Returns

bool

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 double

The amount of capacity to obtain from the token bucket

failFast bool

Indicates that the client should or shouldn't sleep and try again if capacity cannot be obtained.

cancellationToken CancellationToken

Token which can be used to cancel the task.

Returns

Task<bool>

UpdateClientSendingRate(bool)

Updates the sending rate within the client's token bucket

public void UpdateClientSendingRate(bool isThrottlingError)

Parameters

isThrottlingError bool

Indicates 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 int
cancellationToken CancellationToken

Returns

Task