Rate-limit Procedures
SYSADMIN.leakyBucketTryConsume
This procedure implements the Leaky Bucket algorithm to provide cross-procedure rate-limit management.
CREATE FOREIGN PROCEDURE leakyBucketTryConsume(IN name string NOT NULL, IN capacity integer, IN refill integer) OPTIONS (UPDATECOUNT 0)
It takes the following parameters:
Parameter | Description |
---|---|
name | Name of a leaky bucket; can be multiple |
capacity | Number of tokens available |
refill | 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.leakyBucketTryConsum
e
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
1. Creating a leaky bucket with a capacity of 2 and a refill rate of 10 seconds:
CALL "SYSADMIN.leakBucketTryConsume"(
"name" => 'bucket1',
"capacity" => 2,
"refill" => 10
);;
2. Updating a leaky bucket to a capacity of 5 and a refill rate of 15 seconds:
CALL "SYSADMIN.leakBucketTryConsume"(
"name" => 'bucket1',
"capacity" => 5,
"refill" => 15
);;
3. Using a leaky bucket:
CALL "SYSADMIN.leakBucketTryConsume"("name" => 'bucket1');;
SYSADMIN.leakyBucketReset
This procedure destroys a leaky bucket.
CREATE FOREIGN PROCEDURE leakyBucketReset(IN name string NOT NULL)
Example
CALL SYSADMIN.leakyBucketReset("name" => 'bucket1');;