{"id":19756809,"url":"https://github.com/imiphp/imi-rate-limit","last_synced_at":"2025-04-30T12:30:45.105Z","repository":{"id":52214599,"uuid":"164067192","full_name":"imiphp/imi-rate-limit","owner":"imiphp","description":"imi 框架的限流组件，可以针对方法、接口设置限流，通过设置总容量、单位时间内生成填充的数量、每次扣除数量实现限流。QQ群：17916227","archived":false,"fork":false,"pushed_at":"2023-11-10T03:44:31.000Z","size":191,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"2.0","last_synced_at":"2024-05-23T05:11:55.382Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":false,"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/imiphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-01-04T06:24:19.000Z","updated_at":"2023-12-19T02:57:05.000Z","dependencies_parsed_at":"2023-10-11T11:05:38.075Z","dependency_job_id":"bc656613-bc2c-403e-92d1-df169f064008","html_url":"https://github.com/imiphp/imi-rate-limit","commit_stats":{"total_commits":39,"total_committers":2,"mean_commits":19.5,"dds":0.02564102564102566,"last_synced_commit":"a099c3bfc345ca350bc0834474286cd09f3def77"},"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-rate-limit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-rate-limit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-rate-limit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imiphp%2Fimi-rate-limit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imiphp","download_url":"https://codeload.github.com/imiphp/imi-rate-limit/tar.gz/refs/heads/2.0","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224207897,"owners_count":17273674,"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":[],"created_at":"2024-11-12T03:16:58.942Z","updated_at":"2024-11-12T03:16:59.527Z","avatar_url":"https://github.com/imiphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# imi-rate-limit\n\n[![Latest Version](https://img.shields.io/packagist/v/imiphp/imi-rate-limit.svg)](https://packagist.org/packages/imiphp/imi-rate-limit)\n[![Php Version](https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg)](https://secure.php.net/)\n[![Swoole Version](https://img.shields.io/badge/swoole-%3E=4.1.0-brightgreen.svg)](https://github.com/swoole/swoole-src)\n[![IMI License](https://img.shields.io/github/license/imiphp/imi-rate-limit.svg)](https://github.com/imiphp/imi-rate-limit/blob/master/LICENSE)\n\n## 介绍\n\n`imi-rate-limit` 是 `imi` 框架的限流组件，基于 `bandwidth-throttle/token-bucket` 开发。\n\n本组件仅支持使用 `Redis` 作为中间件，可以针对方法、接口设置限流，通过设置`总容量、单位时间内生成填充的数量、每次扣除数量`实现限流。\n\n\u003e 本仓库仅用于浏览，不接受 issue 和 Pull Requests，请前往：\u003chttps://github.com/imiphp/imi\u003e\n\n## Composer\n\n本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容:\n\n```json\n{\n    \"require\": {\n        \"imiphp/imi-rate-limit\": \"~2.0.0\"\n    }\n}\n```\n\n然后执行 `composer update` 安装。\n\n## 使用\n\n在项目 `config/config.php` 中配置：\n\n```php\n[\n    'components'    =\u003e  [\n        // 引入本组件\n        'RateLimit'    =\u003e  'Imi\\RateLimit',\n    ],\n    'pools'    =\u003e    [\n        // 一定得要配置 Redis 连接池才可以用\n    ],\n    'redis' =\u003e  [\n        'defaultPool'   =\u003e  'redis连接池名称',\n    ],\n]\n```\n\n使用：\n\n```php\n/**\n * 限制每秒同时访问 3 次\n * \n * @Action\n * \n * @RateLimit(name=\"test1\", capacity=3)\n *\n * @return void\n */\npublic function test1()\n{\n    return [\n        'data'  =\u003e  'test1',\n    ];\n}\n\n/**\n * 限制每秒同时访问 1 次，等待解除限制后继续执行，超时时间为 1 秒\n * \n * @Action\n * \n * @RateLimit(name=\"test2\", capacity=1)\n * @BlockingConsumer(1)\n *\n * @return void\n */\npublic function test2()\n{\n    return [\n        'data'  =\u003e  'test2',\n    ];\n}\n\n/**\n * 总容量为 1000，每毫秒填充 1，每次调用扣除 500\n * \n * 自定义处理限制\n * \n * @Action\n * \n * @RateLimit(name=\"test3\", capacity=1000, fill=1, unit=\"millisecond\", deduct=500, callback=\"\\ImiDemo\\HttpDemo\\Util\\RateLimitParser::parse\")\n *\n * @return void\n */\npublic function test3()\n{\n    return [\n        'data'  =\u003e  'test3',\n    ];\n}\n\n/**\n * 手动调用限流\n * \n * 总容量为 1000，每毫秒填充 1，每次调用扣除 500\n *\n * @Action\n * \n * @return void\n */\npublic function test4()\n{\n    if(true !== $result = RateLimiter::limit('test4', 1000, function(){\n        // 自定义回调中的返回值，会作为原方法的返回值被返回\n        return [\n            'message'   =\u003e  '自定义触发限流返回内容',\n        ];\n    }, 1, 'millisecond', 500))\n    {\n        return $result;\n    }\n    return [\n        'data'  =\u003e  'test4',\n    ];\n}\n\n/**\n * 手动调用限流\n * \n * 限制每秒同时访问 1 次，等待解除限制后继续执行，超时时间为 1 秒\n *\n * @Action\n * \n * @return void\n */\npublic function test5()\n{\n    if(true !== $result = RateLimiter::limitBlock('test5', 1, function(){\n        // 自定义回调中的返回值，会作为原方法的返回值被返回\n        return [\n            'message'   =\u003e  '自定义触发限流返回内容',\n        ];\n    }, 1, 1, 'second', 1))\n    {\n        return $result;\n    }\n    return [\n        'data'  =\u003e  'test5',\n    ];\n}\n```\n\n## 免费技术支持\n\nQQ群：17916227 [![点击加群](https://pub.idqqimg.com/wpa/images/group.png \"点击加群\")](https://jq.qq.com/?_wv=1027\u0026k=5wXf4Zq)，如有问题会有人解答和修复。\n\n## 运行环境\n\n- [PHP](https://php.net/) \u003e= 7.4\n- [Composer](https://getcomposer.org/) \u003e= 2.0\n- [Swoole](https://www.swoole.com/) \u003e= 4.1.0\n\n## 版权信息\n\n`imi-rate-limit` 遵循 MIT 开源协议发布，并提供免费使用。\n\n## 捐赠\n\n\u003cimg src=\"https://raw.githubusercontent.com/imiphp/imi/2.0/res/pay.png\"/\u003e\n\n开源不求盈利，多少都是心意，生活不易，随缘随缘……\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-rate-limit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimiphp%2Fimi-rate-limit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimiphp%2Fimi-rate-limit/lists"}