{"id":36971663,"url":"https://github.com/oak-labs-io/phalcon-throttler","last_synced_at":"2026-01-13T21:54:30.710Z","repository":{"id":57029713,"uuid":"111824880","full_name":"oak-labs-io/phalcon-throttler","owner":"oak-labs-io","description":"Phalcon Throttler is a Rate Limiter for the PHP Phalcon Framework.","archived":false,"fork":false,"pushed_at":"2018-03-19T11:00:26.000Z","size":11,"stargazers_count":18,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-19T09:44:03.584Z","etag":null,"topics":["phalcon","php","rate-limiter","rate-limiting","throttler"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oak-labs-io.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-23T15:26:08.000Z","updated_at":"2025-01-03T11:54:01.000Z","dependencies_parsed_at":"2022-08-23T18:50:20.601Z","dependency_job_id":null,"html_url":"https://github.com/oak-labs-io/phalcon-throttler","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/oak-labs-io/phalcon-throttler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oak-labs-io%2Fphalcon-throttler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oak-labs-io%2Fphalcon-throttler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oak-labs-io%2Fphalcon-throttler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oak-labs-io%2Fphalcon-throttler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oak-labs-io","download_url":"https://codeload.github.com/oak-labs-io/phalcon-throttler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oak-labs-io%2Fphalcon-throttler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28401953,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T14:36:09.778Z","status":"ssl_error","status_checked_at":"2026-01-13T14:35:19.697Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["phalcon","php","rate-limiter","rate-limiting","throttler"],"created_at":"2026-01-13T21:54:30.000Z","updated_at":"2026-01-13T21:54:30.701Z","avatar_url":"https://github.com/oak-labs-io.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Throttler\n\n[![License](https://poser.pugx.org/oaklabs/phalcon-throttler/license)](https://packagist.org/packages/oaklabs/phalcon-throttler)\n[![Latest Stable Version](https://poser.pugx.org/oaklabs/phalcon-throttler/v/stable)](https://packagist.org/packages/oaklabs/phalcon-throttler)\n[![Latest Unstable Version](https://poser.pugx.org/oaklabs/phalcon-throttler/v/unstable)](https://packagist.org/packages/oaklabs/phalcon-throttler)\n[![Build Status](https://travis-ci.org/oak-labs-io/phalcon-throttler.svg)](https://travis-ci.org/oak-labs-io/phalcon-throttler)\n\n## Introduction\n\nPhalcon Throttler is a Rate Limiter for PHP Phalcon Framework.\n\nIt provides a simple interface to build Rate Limiters using various strategies as well as with a Redis Throttler ready out of the box.\n\nPHP 7.1+ and Phalcon 3.1.2+ are required.\n\n## Installation\n \nThrottler can be installed through Composer, just include `\"oaklabs/phalcon-throttler\": \"^0.1\"` to your composer.json and run `composer update` or `composer install`.\n\n## Usage\n\n### Throttling\n\nPhalcon Throttler comes shipped with a Redis throttler by default. \nIt uses [PhpRedis](https://github.com/phpredis/phpredis) to communicate with the Redis server.\n\nFirst of all we need a `Redis` instance. \nWe can then add a `redis` service in the Phalcon Dependency Injection container\n\n```php\n$di-\u003esetShared('redis', function () use ($config) {\n    $redis = new \\Redis();\n    $redis-\u003epconnect($config-\u003eredis-\u003ehost, $config-\u003eredis-\u003eport);\n    $redis-\u003eauth($config-\u003eredis-\u003epassword);\n\n    return $redis;\n});\n``` \n\nso that it can be used when we want to create an instance of the Redis Throttler.\nWe can set it up in the Dependency Injection container as well\n\n```php\n$di-\u003esetShared('throttler',function() use ($di) {\n    return new OakLabs\\PhalconThrottler\\RedisThrottler($di-\u003eget('redis'), [\n        'bucket_size'  =\u003e 20,\n        'refill_time'  =\u003e 600, // 10m\n        'refill_amount'  =\u003e 10\n    ]);\n});\n```\n\nThe second parameter allows to configure the behaviour of the Throttler:\n\n- *bucket_size*: the number of allowed hits in the period of time of reference\n- *refill_time*: the amount of time after that the counter will completely or partially reset\n- *refill_amount*: the number of hits to be reset every time the refill_time passes \n\nYou are now able to successfully throttle users:\n\n```php\n$throttler = $this-\u003egetDI()-\u003eget('throttler');\n$rateLimit = $throttler-\u003econsume($this-\u003erequest-\u003egetClientAddress());\n\nif ($rateLimit-\u003eisLimited()) {\n    // Do something\n}\n```\n\n### Strategies\n\nThe only question left is: which one is the appropriate place where the check should be performed?\n\nThere is of course not an uniquely valid answer, several places can be used. \n\n**Check in the dispatcher**\n\nA good strategy is to put the check during the Phalcon dispatcher lifecycle.\n\nIn the dependency injection we can use the Phalcon Event Manager to listen to the dispatcher event and bind it to some Security plugin\n\n```php\n$di-\u003esetShared('eventsManager',function() use ($di) {\n    $eventsManager = new \\Phalcon\\Events\\Manager();\n    return $eventsManager;\n});\n\n$di-\u003eset('dispatcher', function () use ($di) {\n    //Create an EventsManager\n    $eventsManager = $di-\u003egetShared('eventsManager');\n\n    $security = new \\MyNamespace\\Security();\n    $eventsManager-\u003eattach('dispatch', $security);\n\n    $dispatcher = new \\Phalcon\\Mvc\\Dispatcher();\n    $dispatcher-\u003esetEventsManager($eventsManager);\n\n    return $dispatcher;\n});\n```\n\nand put our Rate Limiter in it\n\n```php\n\u003c?php\n\nnamespace MyNamespace;\n\nuse Phalcon\\Events\\Event;\nuse Phalcon\\Mvc\\User\\Plugin;\nuse Phalcon\\Mvc\\Dispatcher;\nuse OakLabs\\PhalconThrottler\\ThrottlerInterface;\n\nclass Security extends Plugin\n{\n    public function beforeDispatch(Event $event, Dispatcher $dispatcher)\n    {\n        /** @var ThrottlerInterface $throttler */\n        $throttler = $this-\u003egetDI()-\u003eget('throttler');\n        $rateLimit = $throttler-\u003econsume($this-\u003erequest-\u003egetClientAddress());\n\n        if ($rateLimit-\u003eisLimited()) {\n            $dispatcher-\u003eforward(\n                [\n                    'namespace' =\u003e 'MyNamespace\\Http',\n                    'controller' =\u003e 'error',\n                    'action' =\u003e 'ratelimited',\n                    'params' =\u003e $rateLimit-\u003etoArray()\n                ]\n            );\n        }\n    }\n}\n```\n\nand finally perform a redirection in case the User gets rate limited.\nThe information returned by the `$rateLimit-\u003etoArray()` method contains:\n\n```php\n[\n    'hits' =\u003e (int) // Number of hits in the reference period,\n    'remaining' =\u003e(int) // Remaining hits before getting rate limited,\n    'period' =\u003e (int) // Reference period in seconds,\n    'hits_per_period' =\u003e (int) // Allowed number of hits in the reference period,\n    'warning' =\u003e (bool) // Whether a warning has been emitted,\n    'limited' =\u003e (bool) // Whether the User is rate limited\n]\n```\n\n## Contribution guidelines\n\nThrottler follows PSR-1, PSR-2 and PSR-4 PHP coding standards, and semantic versioning.\n\nPull requests are welcome.\n\n## License\n\nThrottler is free software distributed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foak-labs-io%2Fphalcon-throttler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foak-labs-io%2Fphalcon-throttler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foak-labs-io%2Fphalcon-throttler/lists"}