2017 © Pedro Peláez
 

symfony-bundle rate-limit-bundle

Simple rate limiting based on routes.

image

fusonic/rate-limit-bundle

Simple rate limiting based on routes.

  • Thursday, July 12, 2018
  • by mburtscher
  • Repository
  • 4 Watchers
  • 0 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

RateLimitBundle

This bundle provides simple rate limiting based on routes., (*1)

Getting started

  1. Install bundle:
composer require fusonic/rate-limit-bundle
  1. Add RateLimitBundle to kernel:
    Fusonic\RateLimitBundle\RateLimitBundle::class => ['prod' => true],
  1. Add cache config
framework:
    cache:
        app: cache.adapter.array
  1. Add rate limit config
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), (*2)

Example

Create an event listener or subscriber:, (*3)

<?php

namespace AppBundle\EventListener;

use Fusonic\RateLimitBundle\Event\RateLimitEvents;
use Fusonic\RateLimitBundle\Event\RateLimitExceededEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;

final class RateLimitSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            RateLimitEvents::ROUTE_LIMIT_EXCEEDED => '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., (*4)

    app.rate_limit_subscriber:
        class: AppBundle\EventListener\RateLimitSubscriber
        tags:
            - { name: kernel.event_subscriber }

Execute tests

Run the the tests by executing:, (*5)

vendor/bin/simple-phpunit

The Versions