Download the PHP package reactphp-x/limiter without Composer
On this page you can find all versions of the php package reactphp-x/limiter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download reactphp-x/limiter
More information about reactphp-x/limiter
Files in reactphp-x/limiter
Package limiter
Short Description A generic rate limiter for the web and reactphp. Useful for API clients, web crawling, or other tasks that need to be throttled
License MIT
Informations about the package limiter
reactphp-limiter
Provides a generic rate limiter for the web and node.js. Useful for API clients, web crawling, or other tasks that need to be throttled. Two classes are exposed, RateLimiter and TokenBucket. TokenBucket provides a lower level interface to rate limiting with a configurable burst rate and drip rate. RateLimiter sits on top of the token bucket and adds a restriction on the maximum number of tokens that can be removed each interval to comply with common API restrictions such as "150 requests per hour maximum".
Installation
Usage
A simple example allowing 150 requests per hour:
Another example allowing one message to be sent every 250ms:
The default behaviour is to wait for the duration of the rate limiting that's
currently in effect before the promise is resolved, but if you pass in
"fireImmediately": true
, the promise will be resolved immediately with
remainingRequests
set to -1:
A synchronous method, tryRemoveTokens(), is available in both RateLimiter and TokenBucket. This will return immediately with a boolean value indicating if the token removal was successful.
To get the number of remaining tokens outside the removeTokens
promise,
simply use the getTokensRemaining
method.
Using the token bucket directly to throttle at the byte level:
Additional Notes
Both the token bucket and rate limiter should be used with a message queue or some way of preventing multiple simultaneous calls to removeTokens(). Otherwise, earlier messages may get held up for long periods of time if more recent messages are continually draining the token bucket. This can lead to out of order messages or the appearance of "lost" messages under heavy load.
Tests
License
MIT License