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
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
$cache
Container for throttle counters.
protected
CacheInterface
$cache
$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
__construct()
Constructor.
public
__construct(CacheInterface $cache) : mixed
Parameters
- $cache : CacheInterface
getTokenTime()
Returns the number of seconds until the next available token will be released for usage.
public
getTokenTime() : int
Return values
intremove()
public
remove(string $key) : self
Parameters
- $key : string
-
The name of the bucket
Return values
selfsetTestTime()
Used during testing to set the current timestamp to use.
public
setTestTime(int $time) : $this
Parameters
- $time : int
Return values
$thistime()
Return the test time, defaulting to current.
public
time() : int