jalismrs / symfony.bundle.api-throttler
Adds a service to throttle API calls
This package's canonical repository appears to be gone and the package has been frozen as a result.
Installs: 515
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 4
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^7.4 || ^8.0
- jalismrs/symfony.bundle.core: ^1.0.0
- maba/gentle-force-bundle: ^0.6.3
- symfony/config: ^5.0
- symfony/dependency-injection: ^5.0
- symfony/http-kernel: ^5.0
Requires (Dev)
- composer/package-versions-deprecated: *
- mikey179/vfsstream: *
- phpunit/phpunit: 9.4.2
- roave/security-advisories: dev-master
README
Adds a service to throttle API calls
Test
phpunit
or vendor/bin/phpunit
coverage reports will be available in var/coverage
Requirements
- Redis
Use
use Jalismrs\Symfony\Bundle\JalismrsApiThrottlerBundle\ApiThrottler; class SomeApiClass { private ApiThrottler $apiThrottler; public function __construct( ApiThrottler $apiThrottler ) { $this->apiThrottler = $apiThrottler; /* * register rate limits HERE * https://packagist.org/packages/maba/gentle-force */ $this->apiThrottler->registerRateLimits( 'useCaseKey', // string $useCaseKey [], // array $rateLimits ); } public function someApiCall(): void { $this->apiThrottler->waitAndIncrease( 'useCaseKey', // string $useCaseKey 'identifier', // string $identifier 4, // int|null $cap ); // api call HERE } }
Configuration
Try limits can be configured:
- for all calls
- for all 'useCaseKey' calls
- for all 'useCaseKey.identifier' calls
- for a specific call with $cap parameter
# config/packages/jalismrs_api_throttler.yaml jalismrs_api_throttler: cap: 1 caps: useCaseKey: 2 useCaseKey.identifier: 0 redis: parameters: host: 'redis_host' options: prefix: 'redis_prefix'