Documentation

RdbBucket
in package

Leaky bucket rate limiting.

Table of Contents

Properties

$capacity  : int
$costs  : array<string|int, int>
$drip_rate  : float
$key  : string
$prefix  : string
$drips  : array<string|int, mixed>
$rdb  : Rdb

Methods

__construct()  : mixed
Create a new bucket.
drip()  : bool
Record a drip in to the bucket.
getLevel()  : int
What's the water level of the bucket?
getStatus()  : array<string|int, mixed>
Get bucket info.
getWait()  : float
Time to wait (ms) before the next drip.
isFull()  : bool
Is the bucket full?
refresh()  : void
writeHeaders()  : void
Write out bucket info to the headers.
load()  : array<string|int, mixed>
Get the drips.
purge()  : array<string|int, mixed>
Clean out old drips.
save()  : void
Save the drips.

Properties

$capacity

public int $capacity = 60

Bucket size.

$costs

public array<string|int, int> $costs = []

[ name => drip size ]

$drip_rate

public float $drip_rate = 1

Drips per second.

$drips

protected array<string|int, mixed> $drips = []

Methods

__construct()

Create a new bucket.

public __construct(Rdb $rdb, string|array<string|int, mixed> $config) : mixed

The config is this:

  • key
  • prefix (def: 'drip:')
  • capacity (def: 60)
  • drip_rate (def: 1)
  • costs
Parameters
$rdb : Rdb
$config : string|array<string|int, mixed>

drip()

Record a drip in to the bucket.

public drip([int|string $size = 1 ]) : bool

Either provide a number to specify a number of drips or a string to use a configured 'cost'.

For example, per request:

  • 'GET' might be 1 drip
  • 'POST' is 5 drips

Or, per region:

  • 'AU' is 1 drip
  • 'US' is 2 drips
Parameters
$size : int|string = 1

drop size or cost lookup

  • int - How many drips
  • string - A cost name
Return values
bool —
  • false - the bucket is full, did not add the drip
  • true - the drip was added

getLevel()

What's the water level of the bucket?

public getLevel() : int
Return values
int —

number of drips

getStatus()

Get bucket info.

public getStatus() : array<string|int, mixed>

These are typically used as headers to help a client figure how to not bust the rate limit.

Return values
array<string|int, mixed>

getWait()

Time to wait (ms) before the next drip.

public getWait() : float

This will be zero if the bucket isn't full.

Return values
float —

seconds

isFull()

Is the bucket full?

public isFull() : bool
Return values
bool

refresh()

public refresh() : void
Tags
throws
JsonException

writeHeaders()

Write out bucket info to the headers.

public writeHeaders() : void

load()

Get the drips.

protected load() : array<string|int, mixed>
Return values
array<string|int, mixed>

purge()

Clean out old drips.

protected purge(array<string|int, mixed> $drips) : array<string|int, mixed>
Parameters
$drips : array<string|int, mixed>
Return values
array<string|int, mixed>

save()

Save the drips.

protected save() : void
On this page

Search results