LeakyBucket
QuantConnect.Util.RateLimit.LeakyBucket
LeakyBucket(
capacity: int,
refill_amount: int,
refill_interval: timedelta,
)
LeakyBucket(
capacity: int,
sleep: ISleepStrategy,
refill: IRefillStrategy,
time_provider: ITimeProvider = None,
)
Bases: Object, ITokenBucket
Provides an implementation of ITokenBucket that implements the leaky bucket algorithm See: https://en.wikipedia.org/wiki/Leaky_bucket
Signature descriptions:
-
Initializes a new instance of the LeakyBucket class. This constructor initializes the bucket using the ThreadsleepStrategy.sleep with a 1 millisecond sleep to prevent being CPU intensive and uses the FixedIntervalRefillStrategy to refill bucket tokens according to the refill_amount and refill_interval parameters.
-
Initializes a new instance of the LeakyBucket class
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
capacity
|
int
|
The maximum number of tokens this bucket can hold |
required |
refill_amount
|
Optional[int]
|
The number of tokens to add to the bucket each refill_interval |
None
|
refill_interval
|
Optional[timedelta]
|
The interval which after passing more tokens are added to the bucket |
None
|
sleep
|
Optional[ISleepStrategy]
|
Defines the ISleepStrategy used when consume is invoked |
None
|
refill
|
Optional[IRefillStrategy]
|
Defines the IRefillStrategy that computes how many tokens to add |
None
|
time_provider
|
Optional[ITimeProvider]
|
Defines the ITimeProvider used to enforce timeouts when |
None
|
capacity
capacity: int
Gets the maximum capacity of tokens this bucket can hold.
available_tokens
available_tokens: int
Gets the total number of currently available tokens for consumption
consume
consume(tokens: int, timeout: int = ...) -> None
Blocks until the specified number of tokens are available for consumption and then consumes that number of tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
int
|
The number of tokens to consume |
required |
timeout
|
int
|
The maximum amount of time, in milliseconds, to block. An exception is throw in the event it takes longer than the stated timeout to consume the requested number of tokens |
...
|
try_consume
try_consume(tokens: int) -> bool
Attempts to consume the specified number of tokens from the bucket. If the requested number of tokens are not immediately available, then this method will return false to indicate that zero tokens have been consumed.