CodeIgniter v4.5 API

Throttler
in package
implements ThrottlerInterface

Class Throttler

Uses an implementation of the Token Bucket algorithm to implement a "rolling window" type of throttling that can be used for rate limiting an API or any other request.

Each "token" in the "bucket" is equivalent to a single request for the purposes of this implementation.

Tags
see
https://en.wikipedia.org/wiki/Token_bucket

Table of Contents

Interfaces

ThrottlerInterface
Expected behavior of a Throttler

Properties

$cache  : CacheInterface
Container for throttle counters.
$prefix  : string
The prefix applied to all keys to minimize potential conflicts.
$testTime  : int
Timestamp to use (during testing)
$tokenTime  : int
The number of seconds until the next token is available.

Methods

__construct()  : mixed
Constructor.
getTokenTime()  : int
Returns the number of seconds until the next available token will be released for usage.
remove()  : self
setTestTime()  : $this
Used during testing to set the current timestamp to use.
time()  : int
Return the test time, defaulting to current.

Properties

$prefix

The prefix applied to all keys to minimize potential conflicts.

protected string $prefix = 'throttler_'

$testTime

Timestamp to use (during testing)

protected int $testTime

$tokenTime

The number of seconds until the next token is available.

protected int $tokenTime = 0

Methods

getTokenTime()

Returns the number of seconds until the next available token will be released for usage.

public getTokenTime() : int
Return values
int

remove()

public remove(string $key) : self
Parameters
$key : string

The name of the bucket

Return values
self

setTestTime()

Used during testing to set the current timestamp to use.

public setTestTime(int $time) : $this
Parameters
$time : int
Return values
$this

time()

Return the test time, defaulting to current.

public time() : int
Tags
TODO

should be private

Return values
int

        
On this page

Search results