Rate-limit Procedures
SYSADMIN.leakyBucketTryConsume
This procedure implements the Leaky Bucket algorithm to provide cross-procedure rate-limit management.
SQL
|
It takes the following parameters:
Parameter | Description |
---|---|
| Name of a leaky bucket; can be multiple |
| Number of tokens available |
| Number of seconds to accumulate a unit |
The leakyBucketTryConsume
procedure is used to manage a "bucket" of tokens. When you call this procedure, it will either create a new bucket, update an existing one, or simply consume a token, depending on the parameters you pass.
If you pass all parameters (
name
,capacity
, andrefill
), and the name does not already exist, a new bucket will be created, andSYSADMIN.leakyBucketTryConsume
will be called.If you pass
name
and one or both of the other parameters (capacity
and/orrefill
), and the name already exists, the bucket will be updated, andSYSADMIN.leakyBucketTryConsume
will be called.If you only pass
name
, and the name already exists,SYSADMIN.leakyBucketTryConsume
will be called.
The SYSADMIN.leakyBucketTryConsume
call will either consume a token from the bucket if one is available or wait for a token to be generated before consuming it. When a new bucket is created, or an existing one is updated, it will be filled with tokens.
Please note that a warning will be added to the server log if a bucket's capacity or refill time is modified. The refill time is calculated only when the procedure is called. Both token replenishment and consumption actions are performed in a synchronized block to ensure they are resistant to multithreading.
Examples
Creating a leaky bucket with a capacity of 2 and a refill rate of 10 seconds:
SQL
|
Updating a leaky bucket to a capacity of 5 and a refill rate of 15 seconds:
SQL
|
Using a leaky bucket:
SQL
|
SYSADMIN.leakyBucketReset
This procedure destroys a leaky bucket.
SQL
|
Example
SQL
|