Skip to main content

API Reference

A LeakyBucket is a rate limiter with a buffer.

Constructor​

local config = {
-- configuration options
}

local leakyBucket = LeakyBucket.new(config)

Parameters​

TypeNameDefaultDescription
Dictionaryconfig{}Configuration dictionary

Functions​

Return TypeSignatureDescription
boolfill()Attempt to increase the bucket balance. Returns true if the bucket is overflowing.
intgetBalance()Get the current bucket balance.

Events​

SignatureDescription
overflowed(int balance)Fires when the bucket overflows.

fill​

Attempts to increase the bucket balance and returns true if the bucket is overflowing. If the bucket is overflowing, the bucket balance will only be increased if the fillWhenOverflowing configuration option true.

local leakyBucket = LeakyBucket.new()

local isOverflowing = leakyBucket:fill()

if isOverflowing then
-- handle overflowing bucket
else
-- proceed as normal
end

Returns​

TypeDescription
boolIf true, the bucket is overflowing.

getBalance​

Returns the current bucket balance.

local leakyBucket = LeakyBucket.new()

print(leakyBucket:getBalance()) -- 0

Returns​

TypeDescription
intThe current bucket balance.

overflowed​

This even fires when fill is called while the bucket is full. It provides the current bucket balance.

local config = {
size = 1;
}

local leakyBucket = LeakyBucket.new(config)

local function onOverflowed(balance)
print("Bucket has overflowed to", balance)
end

leakyBucket.overflowed:connect(onOverflowed)

leakyBucket:fill()
leakyBucket:fill()

-- Expected output:
-- Bucket has overflowed to 2

Parameters​

TypeDescription
intThe current bucket balance.