PHPackages                             imiphp/imi-rate-limit - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. imiphp/imi-rate-limit

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

imiphp/imi-rate-limit
=====================

imi 框架的限流组件，可以针对方法、接口设置限流，通过设置总容量、单位时间内生成填充的数量、每次扣除数量实现限流。QQ群：17916227

v2.1.8(2y ago)91.4k31MulanPSL-2.0PHP

Since Jan 4Pushed 2y ago1 watchersCompare

[ Source](https://github.com/imiphp/imi-rate-limit)[ Packagist](https://packagist.org/packages/imiphp/imi-rate-limit)[ RSS](/packages/imiphp-imi-rate-limit/feed)WikiDiscussions 2.0 Synced today

READMEChangelog (10)Dependencies (1)Versions (28)Used By (1)

imi-rate-limit
==============

[](#imi-rate-limit)

[![Latest Version](https://camo.githubusercontent.com/764ee293511817872a1c017eecf1e3e9eab70bb501a45273b1cefd214da913ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d697068702f696d692d726174652d6c696d69742e737667)](https://packagist.org/packages/imiphp/imi-rate-limit)[![Php Version](https://camo.githubusercontent.com/4a5c2ab20974058a8bab53ecb30ac4c2e6bb961df6229b7386fdc097ab53dfa8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e342d627269676874677265656e2e737667)](https://secure.php.net/)[![Swoole Version](https://camo.githubusercontent.com/f077644cadc3b88104d75a54818d0e1462f910dc6d6dfd9939ea295fb11b4e2b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e312e302d627269676874677265656e2e737667)](https://github.com/swoole/swoole-src)[![IMI License](https://camo.githubusercontent.com/4ee612d07e40116e1826004078cf3d8b400c514109064bbdda65160566780bbf/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d697068702f696d692d726174652d6c696d69742e737667)](https://github.com/imiphp/imi-rate-limit/blob/master/LICENSE)

介绍
--

[](#介绍)

`imi-rate-limit` 是 `imi` 框架的限流组件，基于 `bandwidth-throttle/token-bucket` 开发。

本组件仅支持使用 `Redis` 作为中间件，可以针对方法、接口设置限流，通过设置`总容量、单位时间内生成填充的数量、每次扣除数量`实现限流。

> 本仓库仅用于浏览，不接受 issue 和 Pull Requests，请前往：

Composer
--------

[](#composer)

本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容:

```
{
    "require": {
        "imiphp/imi-rate-limit": "~2.0.0"
    }
}
```

然后执行 `composer update` 安装。

使用
--

[](#使用)

在项目 `config/config.php` 中配置：

```
[
    'components'    =>  [
        // 引入本组件
        'RateLimit'    =>  'Imi\RateLimit',
    ],
    'pools'    =>    [
        // 一定得要配置 Redis 连接池才可以用
    ],
    'redis' =>  [
        'defaultPool'   =>  'redis连接池名称',
    ],
]
```

使用：

```
/**
 * 限制每秒同时访问 3 次
 *
 * @Action
 *
 * @RateLimit(name="test1", capacity=3)
 *
 * @return void
 */
public function test1()
{
    return [
        'data'  =>  'test1',
    ];
}

/**
 * 限制每秒同时访问 1 次，等待解除限制后继续执行，超时时间为 1 秒
 *
 * @Action
 *
 * @RateLimit(name="test2", capacity=1)
 * @BlockingConsumer(1)
 *
 * @return void
 */
public function test2()
{
    return [
        'data'  =>  'test2',
    ];
}

/**
 * 总容量为 1000，每毫秒填充 1，每次调用扣除 500
 *
 * 自定义处理限制
 *
 * @Action
 *
 * @RateLimit(name="test3", capacity=1000, fill=1, unit="millisecond", deduct=500, callback="\ImiDemo\HttpDemo\Util\RateLimitParser::parse")
 *
 * @return void
 */
public function test3()
{
    return [
        'data'  =>  'test3',
    ];
}

/**
 * 手动调用限流
 *
 * 总容量为 1000，每毫秒填充 1，每次调用扣除 500
 *
 * @Action
 *
 * @return void
 */
public function test4()
{
    if(true !== $result = RateLimiter::limit('test4', 1000, function(){
        // 自定义回调中的返回值，会作为原方法的返回值被返回
        return [
            'message'   =>  '自定义触发限流返回内容',
        ];
    }, 1, 'millisecond', 500))
    {
        return $result;
    }
    return [
        'data'  =>  'test4',
    ];
}

/**
 * 手动调用限流
 *
 * 限制每秒同时访问 1 次，等待解除限制后继续执行，超时时间为 1 秒
 *
 * @Action
 *
 * @return void
 */
public function test5()
{
    if(true !== $result = RateLimiter::limitBlock('test5', 1, function(){
        // 自定义回调中的返回值，会作为原方法的返回值被返回
        return [
            'message'   =>  '自定义触发限流返回内容',
        ];
    }, 1, 1, 'second', 1))
    {
        return $result;
    }
    return [
        'data'  =>  'test5',
    ];
}
```

免费技术支持
------

[](#免费技术支持)

QQ群：17916227 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq)，如有问题会有人解答和修复。

运行环境
----

[](#运行环境)

- [PHP](https://php.net/) &gt;= 7.4
- [Composer](https://getcomposer.org/) &gt;= 2.0
- [Swoole](https://www.swoole.com/) &gt;= 4.1.0

版权信息
----

[](#版权信息)

`imi-rate-limit` 遵循 MIT 开源协议发布，并提供免费使用。

捐赠
--

[](#捐赠)

[![](https://raw.githubusercontent.com/imiphp/imi/2.0/res/pay.png)](https://raw.githubusercontent.com/imiphp/imi/2.0/res/pay.png)

开源不求盈利，多少都是心意，生活不易，随缘随缘……

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 97.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~65 days

Recently: every ~49 days

Total

28

Last Release

913d ago

Major Versions

v1.0.3 → v2.0.02021-08-20

v2.1.8 → 3.0.x-dev2023-11-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f917bb42280d114c53cebadc2942a13ee03abe14971089f88895e266d637169?d=identicon)[Yurunsoft](/maintainers/Yurunsoft)

---

Top Contributors

[![Yurunsoft](https://avatars.githubusercontent.com/u/20104656?v=4)](https://github.com/Yurunsoft "Yurunsoft (39 commits)")[![NHZEX](https://avatars.githubusercontent.com/u/14545600?v=4)](https://github.com/NHZEX "NHZEX (1 commits)")

### Embed Badge

![Health badge](/badges/imiphp-imi-rate-limit/health.svg)

```
[![Health](https://phpackages.com/badges/imiphp-imi-rate-limit/health.svg)](https://phpackages.com/packages/imiphp-imi-rate-limit)
```

###  Alternatives

[h5p/h5p-editor

H5P Editor functionality in PHP

761.5M29](/packages/h5p-h5p-editor)[bandwidth-throttle/bandwidth-throttle

Bandwidth throttle at application layer

87139.3k](/packages/bandwidth-throttle-bandwidth-throttle)[petrgrishin/array-map

The object oriented approach to working with arrays on PHP

125.8k1](/packages/petrgrishin-array-map)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
