Skip to main content
Skip table of contents

Rate-limit Procedures

SYSADMIN.leakyBucketTryConsume

This procedure implements the Leaky Bucket algorithm to provide cross-procedure rate-limit management.

SQL
CREATE FOREIGN PROCEDURE leakyBucketTryConsume(IN name string NOT NULL, IN capacity integer, IN refill integer) OPTIONS (UPDATECOUNT 0)

It takes the following parameters:

ParameterDescription
nameName of a leaky bucket; can be multiple
capacityNumber of tokens available
refillNumber 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, and refill), and the name does not already exist, a new bucket will be created, and SYSADMIN.leakyBucketTryConsume will be called.
  • If you pass name and one or both of the other parameters (capacity and/or refill), and the name already exists, the bucket will be updated, and SYSADMIN.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

1. Creating a leaky bucket with a capacity of 2 and a refill rate of 10 seconds:

CODE
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:

CODE
CALL "SYSADMIN.leakBucketTryConsume"(
    "name" => 'bucket1',
    "capacity" => 5,
    "refill" => 15
);;

3. Using a leaky bucket:

CODE
CALL "SYSADMIN.leakBucketTryConsume"("name" => 'bucket1');;

SYSADMIN.leakyBucketTryConsume procedure is available since v3.2

SYSADMIN.leakyBucketReset

This procedure destroys a leaky bucket.

SQL
CREATE FOREIGN PROCEDURE leakyBucketReset(IN name string NOT NULL)

Example

SQL
CALL SYSADMIN.leakyBucketReset("name" => 'bucket1');;

SYSADMIN.leakyBucketReset procedure is available since v3.2

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.