{"id":18550297,"url":"https://github.com/xp-forge/ratelimit","last_synced_at":"2025-05-15T10:08:58.595Z","repository":{"id":25301020,"uuid":"28727451","full_name":"xp-forge/ratelimit","owner":"xp-forge","description":"Rate limiting","archived":false,"fork":false,"pushed_at":"2024-03-24T13:13:40.000Z","size":56,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-12T23:02:12.544Z","etag":null,"topics":["ratelimiter","xp-framework"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xp-forge.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-02T21:53:14.000Z","updated_at":"2022-02-28T21:19:51.000Z","dependencies_parsed_at":"2022-09-17T00:50:37.674Z","dependency_job_id":null,"html_url":"https://github.com/xp-forge/ratelimit","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fratelimit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fratelimit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fratelimit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xp-forge%2Fratelimit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xp-forge","download_url":"https://codeload.github.com/xp-forge/ratelimit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254319722,"owners_count":22051075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ratelimiter","xp-framework"],"created_at":"2024-11-06T21:04:08.394Z","updated_at":"2025-05-15T10:08:58.556Z","avatar_url":"https://github.com/xp-forge.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Rate limiting\n=============\n\n[![Build status on GitHub](https://github.com/xp-forge/ratelimit/workflows/Tests/badge.svg)](https://github.com/xp-forge/ratelimit/actions)\n[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)\n[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)\n[![Requires PHP 7.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_0plus.svg)](http://php.net/)\n[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)\n[![Latest Stable Version](https://poser.pugx.org/xp-forge/ratelimit/version.png)](https://packagist.org/packages/xp-forge/ratelimit)\n\nA rate limiting can be used to limit the rate at which physical or logical resources are accessed, e.g. to protect them from intentional or unintentional overuse.\n\nRestricting usage\n-----------------\nImagine you don't want to run more than two tasks per second:\n\n```php\nuse util\\invoke\\RateLimiting;\n\n$rateLimiter= new RateLimiting(2);\nforeach ($tasks as $task) {\n  $rateLimiter-\u003eacquire();    // will wait if necessary\n  $task-\u003erun();\n}\n```\n\nRestricting bandwidth\n---------------------\nYou can implement bandwidth throttling by acquiring a permit for each byte:\n\n```php\nuse util\\invoke\\{RateLimiting, Rate, Per};\n\n$rateLimiter= new RateLimiting(new Rate(1000000, Per::$MINUTE));\nwhile ($bytes= $source-\u003eread()) {\n  $rateLimiter-\u003eacquire(strlen($bytes));\n  $target-\u003ewrite($bytes);\n}\n```\n\nRate-limiting users\n-------------------\nImplement a filter like the following:\n\n```php\nuse web\\{Filter, Error};\nuse util\\invoke\\{RateLimiting, Rate, Per};\n\nclass RateLimitingFilter implements Filter {\n  private $rates, $rate, $timeout;\n\n  public function __construct(KeyValueStorage $rates) {\n    $this-\u003erates= $rates;\n    $this-\u003erate= new Rate(5000, Per::$HOUR);\n    $this-\u003etimeout= 0.2;\n  }\n\n  public function filter($request, $response, $invocation) {\n    $remote= $request-\u003eheader('Remote-Addr');\n\n    $limits= $this-\u003erates-\u003eget($remote) ?: new RateLimiting($this-\u003erate);\n    $permitted= $limits-\u003etryAcquiring(1, $this-\u003etimeout);\n    $this-\u003erates-\u003eput($remote, $limits);\n\n    $response-\u003eheader('X-RateLimit-Limit', $limits-\u003erate()-\u003evalue());\n    $response-\u003eheader('X-RateLimit-Remaining', $limits-\u003eremaining());\n    $response-\u003eheader('X-RateLimit-Reset', $limits-\u003eresetTime());\n\n    if (!$permitted) {\n      throw new Error(429, 'Rate limit exceeded');\n    }\n\n    return $invocation-\u003eproceed($request, $response);\n  }\n}\n```\n\nFurther reading\n---------------\n\n* [RateLimiter - discovering Google Guava](http://www.nurkiewicz.com/2012/09/ratelimiter-discovering-google-guava.html) by Tomasz Nurkiewicz\n* [Guava's RateLimiter class](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/util/concurrent/RateLimiter.html) - which is what this project was inspired from.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fratelimit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxp-forge%2Fratelimit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxp-forge%2Fratelimit/lists"}