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:

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

SQL
CALL "SYSADMIN.leakBucketTryConsume"(
    "name" => 'bucket1',
    "capacity" => 2,
    "refill" => 10
);;
  1. Updating a leaky bucket to a capacity of 5 and a refill rate of 15 seconds:

SQL
CALL "SYSADMIN.leakBucketTryConsume"(
    "name" => 'bucket1',
    "capacity" => 5,
    "refill" => 15
);;
  1. Using a leaky bucket:

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

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');;

JavaScript errors detected

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

If this problem persists, please contact our support.