https://github.com/kelunik/rate-limit
Rate Limiting for Amp.
https://github.com/kelunik/rate-limit
amphp php rate-limit
Last synced: 9 days ago
JSON representation
Rate Limiting for Amp.
- Host: GitHub
- URL: https://github.com/kelunik/rate-limit
- Owner: kelunik
- License: mit
- Created: 2016-04-09T19:36:20.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2023-09-04T17:58:33.000Z (over 2 years ago)
- Last Synced: 2024-10-10T18:50:51.512Z (over 1 year ago)
- Topics: amphp, php, rate-limit
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 12
- Watchers: 3
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
- awesome-amphp - kelunik/rate-limit - Rate Limiting (Throttling / Tunnel)
README
# rate-limit

`kelunik/rate-limit` is a rate limiting library for [Amp](https://github.com/amphp/amp).
## Installation
```bash
composer require kelunik/rate-limit
```
## Usage
You're in full control of any actions when the rate limit is exceeded. You can also already warn the user before he exceeds the limit.
```php
$current = $this->rateLimit->increment("{$userId}:{$action}");
if ($current > $this->limit) {
// show captcha or error page or do anything you want
} else {
// request is within the limit, continue normally
}
```
If you want to expose the limits, e.g. in an HTTP API, you can also request the reset time for a given key.
```php
$current = $this->rateLimit->increment("{$userId}:{$action}");
$response->setHeader("x-ratelimit-limit", $this->limit);
$response->setHeader("x-ratelimit-remaining", $this->limit - $current);
$response->setHeader("x-ratelimit-reset", $this->rateLimit->getTtl("{$userId}:{$action}"));
```
`RateLimit::getTtl()` returns the seconds until the limit is reset. If you want to return the absolute time, you can just add `time()` to that value.