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
capacityuintMaximum number of tokens the bucket can hold.
refreshRateTimeSpanThe time it takes to refresh one unit (e.g., TimeSpan.FromMilliseconds(200) means 5 units/sec).
initialTokensuintThe number of initial tokens in the bucket.
Properties
Available
Number of available tokens.
public double Available { get; }
Property Value
Methods
Process(double)
Advances the elapsed time.
public void Process(double elapsed)
Parameters
elapseddoubleThe elapsed time in seconds.
Process(TimeSpan)
Advances the elapsed time.
public void Process(TimeSpan elapsed)
Parameters
elapsedTimeSpanThe elapsed time.
Reset(ResetBehavior)
Resets the elapsed time (and optionally the token count).
public void Reset(BucketRateLimiter.ResetBehavior behavior = ResetBehavior.Empty)
Parameters
behaviorBucketRateLimiter.ResetBehaviorThe token count behavior.
TryTake(double, uint)
Attempts to take n tokens from the bucket.
public bool TryTake(double elapsed, uint n = 1)
Parameters
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
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
nuintThe number of tokens to take.
Returns
- bool
True if successful, false if not enough tokens are available.