https://github.com/fusonic/symfony-rate-limit-bundle
https://github.com/fusonic/symfony-rate-limit-bundle
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/fusonic/symfony-rate-limit-bundle
- Owner: fusonic
- License: mit
- Created: 2018-06-04T14:59:41.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-12-11T11:14:00.000Z (over 5 years ago)
- Last Synced: 2025-06-30T05:53:33.869Z (about 1 year ago)
- Language: PHP
- Size: 27.3 KB
- Stars: 0
- Watchers: 5
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# RateLimitBundle
This bundle provides simple rate limiting based on routes.
## Getting started
1. Install bundle:
```
composer require fusonic/rate-limit-bundle
```
2. Add RateLimitBundle to kernel:
```PHP
Fusonic\RateLimitBundle\RateLimitBundle::class => ['prod' => true],
```
3. Add cache config
```YAML
framework:
cache:
app: cache.adapter.array
```
4. Add rate limit config
```YAML
fusonic_rate_limit:
cache_provider: "cache.app"
enabled: true
routes:
foo:
limit: 2
period: 3600
```
## How does it work
The bundle makes use of Symfony's event system. Therefore some events exist under `Fusonic/RateLimitBundle/Event`:
- **RateLimitAttemptsUpdatedEvent** will be emitted when a request for a rate limited route is detected.
- **RateLimitExceededEvent** will be emitted when a route limit is exceeded.
- **RateLimitResetAttemptsEvent** can be used to reset the state for a specific route (e.g. after a successful login)
### Example
Create an event listener or subscriber:
```PHP
'onLimitExceeded',
];
}
public function onLimitExceeded(RateLimitExceededEvent $event): void
{
$config = $event->getRouteLimitConfig();
$message = 'You sent too many requests for this endpoint.';
throw new TooManyRequestsHttpException($config->getPeriod(), $message);
}
}
```
And register it as service.
```YAML
app.rate_limit_subscriber:
class: AppBundle\EventListener\RateLimitSubscriber
tags:
- { name: kernel.event_subscriber }
```
## Execute tests
Run the the tests by executing:
```
vendor/bin/simple-phpunit
```