Table of Contents

Class BucketRateLimiter

Namespace
MarymoorStudios.Core
Assembly
MarymoorStudios.Core.dll

A leaky bucket rate limiter.

public class BucketRateLimiter
Inheritance
BucketRateLimiter
Inherited Members

Constructors

BucketRateLimiter(uint, TimeSpan, uint)

Initializes a new instance of the BucketRateLimiter class.

public BucketRateLimiter(uint capacity, TimeSpan refreshRate, uint initialTokens = 0)

Parameters

capacity uint

Maximum number of tokens the bucket can hold.

refreshRate TimeSpan

The time it takes to refresh one unit (e.g., TimeSpan.FromMilliseconds(200) means 5 units/sec).

initialTokens uint

The number of initial tokens in the bucket.

Properties

Available

Number of available tokens.

public double Available { get; }

Property Value

double

Methods

Process(double)

Advances the elapsed time.

public void Process(double elapsed)

Parameters

elapsed double

The elapsed time in seconds.

Process(TimeSpan)

Advances the elapsed time.

public void Process(TimeSpan elapsed)

Parameters

elapsed TimeSpan

The elapsed time.

Reset(ResetBehavior)

Resets the elapsed time (and optionally the token count).

public void Reset(BucketRateLimiter.ResetBehavior behavior = ResetBehavior.Empty)

Parameters

behavior BucketRateLimiter.ResetBehavior

The token count behavior.

TryTake(double, uint)

Attempts to take n tokens from the bucket.

public bool TryTake(double elapsed, uint n = 1)

Parameters

elapsed double

The elapsed time in seconds.

n uint

The number of tokens to take.

Returns

bool

True if successful, false if not enough tokens are available.

TryTake(TimeSpan, uint)

Attempts to take n tokens from the bucket.

public bool TryTake(TimeSpan elapsed, uint n = 1)

Parameters

elapsed TimeSpan

The elapsed time.

n uint

The number of tokens to take.

Returns

bool

True if successful, false if not enough tokens are available.

TryTake(uint)

Attempts to take n tokens from the bucket.

public bool TryTake(uint n = 1)

Parameters

n uint

The number of tokens to take.

Returns

bool

True if successful, false if not enough tokens are available.