Download the PHP package robwittman/leaky-bucket-rate-limiter without Composer
On this page you can find all versions of the php package robwittman/leaky-bucket-rate-limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download robwittman/leaky-bucket-rate-limiter
More information about robwittman/leaky-bucket-rate-limiter
Files in robwittman/leaky-bucket-rate-limiter
Package leaky-bucket-rate-limiter
Short Description PSR-7 Middleware for implementing a Leaky Bucket Rate Limiter
License
Informations about the package leaky-bucket-rate-limiter
PSR-7 Leaky Bucket Rate Limiter
This middleware enables API Rate-Limiting based on a Leaky Bucket algorithm.
Usage
To get started, you can easily use composer:
composer require robwittman/leaky-bucket-rate-limiter
Once installed, require the package, apply some settings, and start limiting.
The only required settings to use RateLimiter is a callback and throttle.
Examples
IP Address
Session ID
Request Attribute
Once the bucket has a token to act on, it communicates with Redis to keep track of traffic. If the token is over it's request limit, it will trigger the throttle
function passed to the constructor.
Parameters
Callback (required)
The callback argument is called when the Limiter needs a key to check. It passes along the Request object, and can either return an array with a (string) 'token' key, or can return TRUE to skip rate limiting
Throttle (required)
Tell the Limiter how to respond to throttled requests
NOTE All further settings assume callback
and throttle
parameters are already set
Capacity and Leak
Capacity is the total amount of drips (requests) the bucket may contain. Leak is the amount of drips per second that you want to remove from the bucket
Ignored routes
You can pass an array of routes that you do not want to rate limit. This completely bypasses the rate limit middleware, so they will not have respective headers either
Prefix / Postfix
Provide a prefix / suffix for the bucket key. The key will be stored in Redis as PREFIX.key.SUFFIX
Header
Specify what header to provide, containing Rate Limiting info. Set to false to disable.
Storage
By default, the Rate Limiter will attempt to connect to a local redis instance at http://127.0.0.1:6379, as per Predis\Client()
.This can be overridden by providing either an array of settings for Predis\Client
to connect with,
or providing an object with methods get() and set() for storing and retrieving data (mysql, memcached, mongo, etc). If using docker-compose development container, just use redis
as the hostname, and container linking will connect it.
Development / Testing
This library comes packaged with a Docker environment for testing and development. If you're not using Docker, you ought to be!
To bootstrap an environment using docker-compose, simply
docker-compose up
This generates a PHP container with source code and packages, running a local dev server. It also provisions and links a Redis container to use as your storage mechanism.
If you're not using docker-compose, or want to implement a different storage system, you can launch a solo container.
The server can be accessed at :8001, and contains a mini app to play around with. Running tests is equally as easy, and is centered around docker
All versions of leaky-bucket-rate-limiter with dependencies
predis/predis Version ^1.1
zendframework/zend-diactoros Version ^1.3