Repository: spatie/guzzle-rate-limiter-middleware Branch: main Commit: 3626aa5bb584 Files: 21 Total size: 19.9 KB Directory structure: gitextract_wrtnnr9r/ ├── .editorconfig ├── .gitattributes ├── .gitignore ├── .scrutinizer.yml ├── .styleci.yml ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── phpunit.xml.dist ├── src/ │ ├── Deferrer.php │ ├── InMemoryStore.php │ ├── RateLimiter.php │ ├── RateLimiterMiddleware.php │ ├── SleepDeferrer.php │ └── Store.php └── tests/ ├── RateLimiterMiddlewareTest.php ├── RateLimiterTest.php ├── TestCase.php └── TestDeferrer.php ================================================ FILE CONTENTS ================================================ ================================================ FILE: .editorconfig ================================================ ; This file is for unifying the coding style for different editors and IDEs. ; More information at https://editorconfig.org root = true [*] charset = utf-8 indent_size = 4 indent_style = space end_of_line = lf insert_final_newline = true trim_trailing_whitespace = true [*.md] trim_trailing_whitespace = false ================================================ FILE: .gitattributes ================================================ # Path-based git attributes # https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # Ignore all test and documentation with "export-ignore". /.gitattributes export-ignore /.gitignore export-ignore /.travis.yml export-ignore /phpunit.xml.dist export-ignore /.scrutinizer.yml export-ignore /tests export-ignore /.editorconfig export-ignore ================================================ FILE: .gitignore ================================================ build composer.lock docs vendor coverage .phpunit.result.cache ================================================ FILE: .scrutinizer.yml ================================================ filter: excluded_paths: [tests/*] checks: php: remove_extra_empty_lines: true remove_php_closing_tag: true remove_trailing_whitespace: true fix_use_statements: remove_unused: true preserve_multiple: false preserve_blanklines: true order_alphabetically: true fix_php_opening_tag: true fix_linefeed: true fix_line_ending: true fix_identation_4spaces: true fix_doc_comments: true ================================================ FILE: .styleci.yml ================================================ preset: laravel disabled: - single_class_element_per_statement ================================================ FILE: .travis.yml ================================================ language: php php: - 7.1 - 7.2 - 7.3 - 8.0 - 8.1 - 8.2 - 8.3 - 8.4 env: matrix: - COMPOSER_FLAGS="--prefer-lowest" - COMPOSER_FLAGS="" before_script: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source script: - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover after_script: - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover ================================================ FILE: CHANGELOG.md ================================================ # Changelog All notable changes to `guzzle-rate-limiter-middleware` will be documented in this file ## 2.0.1 - 2020-12-19 - add support for PHP 8 ## 2.0.0 - 2020-10-06 - Pass a `$limit` parameter in `Store::push`, the signature is now `Store::push(int $timestamp, int $limit)` - Allow custom `Deferrer` instances to be passed to `RateLimiterMiddleware::perMinute` and `RateLimiterMiddleware::perSecond` ## 1.0.8 - 2020-07-16 - Allow Guzzle 7 ## 1.0.7 - 2020-01-09 - Cast return value of SleepDeferrer to int (#7) ## 1.0.1 - 2019-10-25 - use usleep to ensure sleep actually happens (#5) ## 1.0.0 - 2019-10-08 - initial release ================================================ FILE: LICENSE.md ================================================ The MIT License (MIT) Copyright (c) Spatie bvba Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # A rate limiter middleware for Guzzle [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/guzzle-rate-limiter-middleware.svg?style=flat-square)](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware) [![Build Status](https://img.shields.io/travis/spatie/guzzle-rate-limiter-middleware/master.svg?style=flat-square)](https://travis-ci.org/spatie/guzzle-rate-limiter-middleware) [![Quality Score](https://img.shields.io/scrutinizer/g/spatie/guzzle-rate-limiter-middleware.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/guzzle-rate-limiter-middleware) [![StyleCI](https://github.styleci.io/repos/165693657/shield?branch=master)](https://github.styleci.io/repos/165693657) [![Total Downloads](https://img.shields.io/packagist/dt/spatie/guzzle-rate-limiter-middleware.svg?style=flat-square)](https://packagist.org/packages/spatie/guzzle-rate-limiter-middleware) A rate limiter middleware for Guzzle. Here's what you need to know: - Specify a maximum amount of requests per minute or per second - When the limit is reached, the process will `sleep` until the request can be made - Implement your own driver to persist the rate limiter's request store. This is necessary if the rate limiter needs to work across separate processes, the package ships with an `InMemoryStore`. ## Support us [](https://spatie.be/github-ad-click/guzzle-rate-limiter-middleware) We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). ## Installation You can install the package via composer: ```bash composer require spatie/guzzle-rate-limiter-middleware ``` ## Usage Create a [Guzzle middleware stack](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html) and register it on the client. ```php use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Spatie\GuzzleRateLimiterMiddleware\RateLimiterMiddleware; $stack = HandlerStack::create(); $stack->push(RateLimiterMiddleware::perSecond(3)); $client = new Client([ 'handler' => $stack, ]); ``` You can create a rate limiter to limit per second or per minute. ```php RateLimiterMiddleware::perSecond(3); // Max. 3 requests per second RateLimiterMiddleware::perMinute(5); // Max. 5 requests per minute ``` ## Custom stores By default, the rate limiter works in memory. This means that if you have a second PHP process (or Guzzle client) consuming the same API, you'd still possibly hit the rate limit. To work around this issue, the rate limiter's state should be persisted to a cache. Implement the `Store` interface with your own cache, and pass the store to the rate limiter. ```php use MyApp\RateLimiterStore; use Spatie\GuzzleRateLimiterMiddleware\RateLimit; RateLimiterMiddleware::perSecond(3, new RateLimiterStore()); ``` A Laravel example of a custom `Store`: ```php get(), [$timestamp])); } } ``` ### Testing ``` bash composer test ``` ### Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. ## Contributing Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. ### Security If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. ## Postcardware You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium. We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards). ## Credits - [Sebastian De Deyne](https://github.com/sebastiandedeyne) - [All Contributors](../../contributors) ## License The MIT License (MIT). Please see [License File](LICENSE.md) for more information. ================================================ FILE: composer.json ================================================ { "name": "spatie/guzzle-rate-limiter-middleware", "description": "A rate limiter for Guzzle", "keywords": [ "spatie", "guzzle-rate-limiter-middleware" ], "homepage": "https://github.com/spatie/guzzle-rate-limiter-middleware", "license": "MIT", "authors": [ { "name": "Sebastian De Deyne", "email": "sebastiandedeyne@gmail.com", "homepage": "https://spatie.be", "role": "Developer" } ], "require": { "php": "^7.1|^8.0", "guzzlehttp/guzzle": "^6.3|^7.0" }, "require-dev": { "larapack/dd": "^1.0", "phpunit/phpunit": "^9.3.3" }, "autoload": { "psr-4": { "Spatie\\GuzzleRateLimiterMiddleware\\": "src" } }, "autoload-dev": { "psr-4": { "Spatie\\GuzzleRateLimiterMiddleware\\Tests\\": "tests" } }, "scripts": { "test": "vendor/bin/phpunit", "test-coverage": "vendor/bin/phpunit --coverage-html coverage" }, "config": { "sort-packages": true } } ================================================ FILE: phpunit.xml.dist ================================================ tests src/ ================================================ FILE: src/Deferrer.php ================================================ timestamps; } public function push(int $timestamp, int $limit) { $this->timestamps[] = $timestamp; } } ================================================ FILE: src/RateLimiter.php ================================================ limit = $limit; $this->timeFrame = $timeFrame; $this->store = $store; $this->deferrer = $deferrer; } public function handle(callable $callback) { $delayUntilNextRequest = $this->delayUntilNextRequest(); if ($delayUntilNextRequest > 0) { $this->deferrer->sleep($delayUntilNextRequest); } $this->store->push( $this->deferrer->getCurrentTime(), $this->limit ); return $callback(); } protected function delayUntilNextRequest(): int { $currentTimeFrameStart = $this->deferrer->getCurrentTime() - $this->timeFrameLengthInMilliseconds(); $requestsInCurrentTimeFrame = array_values(array_filter( $this->store->get(), function (int $timestamp) use ($currentTimeFrameStart) { return $timestamp >= $currentTimeFrameStart; } )); if (count($requestsInCurrentTimeFrame) < $this->limit) { return 0; } $oldestRequestStartTimeRelativeToCurrentTimeFrame = $this->deferrer->getCurrentTime() - $requestsInCurrentTimeFrame[0]; return $this->timeFrameLengthInMilliseconds() - $oldestRequestStartTimeRelativeToCurrentTimeFrame; } protected function timeFrameLengthInMilliseconds(): int { if ($this->timeFrame === self::TIME_FRAME_MINUTE) { return 60 * 1000; } return 1000; } } ================================================ FILE: src/RateLimiterMiddleware.php ================================================ rateLimiter = $rateLimiter; } public static function perSecond(int $limit, ?Store $store = null, ?Deferrer $deferrer = null): RateLimiterMiddleware { $rateLimiter = new RateLimiter( $limit, RateLimiter::TIME_FRAME_SECOND, $store ?? new InMemoryStore(), $deferrer ?? new SleepDeferrer() ); return new static($rateLimiter); } public static function perMinute(int $limit, ?Store $store = null, ?Deferrer $deferrer = null): RateLimiterMiddleware { $rateLimiter = new RateLimiter( $limit, RateLimiter::TIME_FRAME_MINUTE, $store ?? new InMemoryStore(), $deferrer ?? new SleepDeferrer() ); return new static($rateLimiter); } public function __invoke(callable $handler) { return function (RequestInterface $request, array $options) use ($handler) { return $this->rateLimiter->handle(function () use ($request, $handler, $options) { return $handler($request, $options); }); }; } } ================================================ FILE: src/SleepDeferrer.php ================================================ assertInstanceOf( RateLimiterMiddleware::class, RateLimiterMiddleware::perSecond(5) ); $this->assertInstanceOf( RateLimiterMiddleware::class, RateLimiterMiddleware::perMinute(5) ); } } ================================================ FILE: tests/RateLimiterTest.php ================================================ createRateLimiter(3, RateLimiter::TIME_FRAME_SECOND); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(100, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(200, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(300, $this->deferrer->getCurrentTime()); $this->deferrer->sleep(700); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(1100, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(1200, $this->deferrer->getCurrentTime()); } /** @test */ public function it_defers_actions_when_it_reaches_a_limit_in_seconds() { $rateLimiter = $this->createRateLimiter(3, RateLimiter::TIME_FRAME_SECOND); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(1000, $this->deferrer->getCurrentTime()); } /** @test */ public function it_execute_actions_below_a_limit_in_minutes() { $rateLimiter = $this->createRateLimiter(3, RateLimiter::TIME_FRAME_MINUTE); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(100, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(200, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(300, $this->deferrer->getCurrentTime()); $this->deferrer->sleep(59700); $rateLimiter->handle(function () { $this->deferrer->sleep(100); }); $this->assertEquals(60100, $this->deferrer->getCurrentTime()); } /** @test */ public function it_defers_actions_when_it_reaches_a_limit_in_minutes() { $rateLimiter = $this->createRateLimiter(3, RateLimiter::TIME_FRAME_MINUTE); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(0, $this->deferrer->getCurrentTime()); $rateLimiter->handle(function () { }); $this->assertEquals(60000, $this->deferrer->getCurrentTime()); } } ================================================ FILE: tests/TestCase.php ================================================ deferrer = new TestDeferrer(); } public function createRateLimiter(int $limit, string $timeFrame): RateLimiter { return new RateLimiter($limit, $timeFrame, new InMemoryStore(), $this->deferrer); } } ================================================ FILE: tests/TestDeferrer.php ================================================ currentTime; } public function sleep(int $milliseconds) { $this->currentTime += $milliseconds; } }