Download the PHP package bayfrontmedia/leaky-bucket without Composer
On this page you can find all versions of the php package bayfrontmedia/leaky-bucket. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bayfrontmedia/leaky-bucket
More information about bayfrontmedia/leaky-bucket
Files in bayfrontmedia/leaky-bucket
Package leaky-bucket
Short Description Framework-agnostic throttling using the leaky bucket algorithm.
License MIT
Homepage https://github.com/bayfrontmedia/leaky-bucket
Informations about the package leaky-bucket
Leaky Bucket
Framework-agnostic throttling using the leaky bucket algorithm.
A bucket has a defined capacity and leak rate per minute. Buckets can also store additional arbitrary data.
- License
- Author
- Requirements
- Installation
- Usage
License
This project is open source and available under the MIT License.
Author
Requirements
- PHP
^8.0
- PDO PHP extension
- JSON PHP extension
Installation
Usage
Storage adapter
A Bayfront\LeakyBucket\ApadpterInterface
must be passed to the Bayfront\LeakyBucket\Bucket
constructor.
There are a variety of storage adapters available, each with their own required configuration.
Flysystem
The Flysystem
adapter allows you to use a Flysystem League\Flysystem\Filesystem
v3 instance for bucket storage.
Local
The Local
adapter allows you to store buckets locally using native PHP.
PDO
The PDO
adapter allows you to use a \PDO
instance for bucket storage into a database.
The PDO
adapter will create/use a table named "buckets" unless otherwise specified in the constructor.
Use the up
method to create the necessary database table to be used by this adapter.
Use the down
method to remove the database table.
These methods throw a Bayfront\LeakyBucket\AdapterException
exception.
Start using Leaky Bucket
Once your adapter has been created, it can be used with Leaky Bucket. In addition, an optional settings array can be passed to the constructor. The default settings are shown below.
NOTE:
Be sure to leak()
the bucket before attempting to do any calculations regarding its capacity.
Also, the save()
method must be used to store the current bucket settings for future use.
Public methods
- exists
- save
- get
- reset
- delete
- isFull
- getCapacity
- getCapacityUsed
- getCapacityRemaining
- hasCapacity
- getLeakPerSecond
- getSecondsPerDrop
- getSecondsUntilCapacity
- getSecondsUntilEmpty
- touch
- getLastTime
- fill
- leak
- spill
- overflow
- dump
- hasData
- setData
- getData
- forgetData
exists
Description:
Checks if this bucket ID already exists in storage.
Parameters:
- None
Returns:
- (bool)
Example:
save
Description:
Saves the bucket.
Parameters:
- None
Returns:
- (self)
Throws:
Bayfront\LeakyBucket\AdapterException
Example:
get
Description:
Returns entire bucket contents.
Parameters:
- None
Returns:
- (array)
Example:
reset
Description:
Reset all bucket information and data.
Parameters:
- None
Returns:
- (self)
Example:
delete
Description:
Resets bucket and deletes the file in storage.
Parameters:
- None
Returns:
- (self)
Throws:
Bayfront\LeakyBucket\AdapterException
Example:
isFull
Description:
Checks if bucket is full.
Parameters:
- None
Returns:
- (bool)
Example:
getCapacity
Description:
Returns the total bucket capacity.
Parameters:
- None
Returns:
- (int)
Example:
getCapacityUsed
Description:
Returns the number of drops in the bucket.
Parameters:
- None
Returns:
- (float)
Example:
getCapacityRemaining
Description:
Returns the remaining bucket capacity.
Parameters:
- None
Returns:
- (float)
Example:
hasCapacity
Description:
Checks if bucket has the capacity fo fill by a given number of drops.
Parameters:
$drops = 1
(int)
Returns:
- (bool)
Example:
getLeakPerSecond
Description:
Returns the number of drops per second the bucket will leak.
Parameters:
- None
Returns:
- (float)
Example:
getSecondsPerDrop
Description:
Returns the number of seconds required to leak one drop.
Parameters:
- None
Returns:
- (float)
Example:
getSecondsUntilCapacity
Description:
Returns the number of seconds until bucket has capacity for number of drops.
Returns 0
if bucket has capacity.
Parameters:
$drops = 1
(int)
Returns:
- (float)
Example:
getSecondsUntilEmpty
Description:
Returns the number of seconds until bucket would be empty.
Parameters:
- None
Returns:
- (float)
Example:
touch
Description:
Manually update the bucket's timestamp.
The bucket's timestamp is automatically updated when any of the following methods are called:
- fill
- leak
- spill
- dump
Parameters:
- None
Returns:
- (self)
Example:
getLastTime
Description:
Returns the bucket's last timestamp.
Parameters:
- None
Returns:
- (int)
Example:
fill
Description:
Fills the bucket with a given number of drops.
If not allowed to overflow, and the bucket does not have the required capacity,
a Bayfront\LeakyBucket\BucketException
will be thrown.
Otherwise, the bucket will be allowed to overflow.
Parameters:
$drops = 1
(int)$allow_overflow = false
(bool)
Returns:
- (self)
Throws:
Bayfront\LeakyBucket\BucketException
Example:
leak
Description:
Updates the bucket by calculating how many drops to leak since it's last timestamp.
Parameters:
- None
Returns:
- (self)
Example:
spill
Description:
Spills a given number of drops from the bucket.
Parameters:
$drops = 1
(int)
Returns:
- (self)
Example:
overflow
Description:
Dumps (empties) all drops from the bucket in excess of its capacity.
Parameters:
- None
Returns:
- (self)
Example:
dump
Description:
Dumps (empties) all drops from the bucket.
Parameters:
- None
Returns:
- (self)
Example:
hasData
Description:
Checks if this bucket contains any additional data, or a specific key in dot notation.
Parameters:
$key = NULL
(string|null): IfNULL
, checks if any additional data exists
Returns:
- (bool)
Example:
setData
Description:
Sets additional data for this bucket in dot notation.
Parameters:
$key
(string)$value
(mixed)
Returns:
- (self)
Example:
getData
Description:
Returns this bucket's additional data key in dot notation, or an optional default value if not found.
Parameters:
$key = NULL
(string|null): Returns the entire data array whenNULL
$default = NULL
(mixed)
Returns:
- (mixed)
Example:
forgetData
Description:
Removes additional data key in dot notation for this bucket.
Parameters:
$key = NULL
(string|null): Removes the entire data array whenNULL
Returns:
- (self)
Example:
All versions of leaky-bucket with dependencies
ext-pdo Version *
ext-json Version *
bayfrontmedia/php-array-helpers Version ^2.0
league/flysystem Version ^3.14