百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

RateLimitBundle

> 编程语言
开源

通过注解轻松为您的控制器/操作添加速率限制

333 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

通过注解轻松为您的控制器/操作添加速率限制

NoxlogicRateLimitBundle

This bundle provides enables the #[RateLimit()] attribute which allows you to limit the number of connections to actions. This is mostly useful in APIs.

The bundle is prepared to work by default in cooperation with the FOSOAuthServerBundle. It contains a listener that adds the OAuth token to the cache-key. However, you can create your own key generator to allow custom rate limiting based on the request. See Create a custom key generator below.

This bundle is partially inspired by a GitHub gist from Ruud Kamphuis.

Features

  • Simple usage through attributes
  • Customize rates per controller, action and even per HTTP method
  • Multiple storage backends: Redis, Memcached and Doctrine cache

Installation

Installation takes just few easy steps:

Step 1: Install the bundle using composer

If you're not yet familiar with Composer see http://getcomposer.org. Tell composer to download the bundle by running the command:

bash
composer require noxlogic/ratelimit-bundle

Step 2: Enable the bundle

If you are using symfony/flex you can skip this step, the bundle will be enabled automatically, otherwise you need to enable the bundle by adding it to the bundles.php file of your project.

php
 ['all' => true],
    // ..
];

Step 3: Install a storage engine

Redis

If you want to use Redis as your storage engine, you might want to install SncRedisBundle:

  • https://github.com/snc/SncRedisBundle

Memcache

If you want to use Memcache, you might want to install LswMemcacheBundle

  • https://github.com/LeaseWeb/LswMemcacheBundle

Doctrine cache

If you want to use Doctrine cache as your storage engine, you might want to install DoctrineCacheBundle:

  • https://github.com/doctrine/DoctrineCacheBundle

Referer to their documentations for more details. You can change your storage engine with the storage_engine configuration parameter. See Configuration reference.

Configuration

Enable bundle only in production

If you wish to enable the bundle only in production environment (so you can test without worrying about limit in your development environments), you can use the enabled configuration setting to enable/disable the bundle completely. It's enabled by default:

yaml
# config_dev.yml
noxlogic_rate_limit:
    enabled: false

Configuration reference

This is the default bundle configuration:

…

Usage

Simple rate limiting

To enable rate limiting, you only need to add the attribute to the specified action

php
generateKey();

        $event->addToKey($key);
        // $event->setKey($key); // to overwrite key completely
    }
}

Make sure to generate a key based on what is rate limited in your controllers.

And example of a IP-based key generator can be:

php
getRequest();
        $event->addToKey($request->getClientIp());
    }
}

Throwing exceptions

Instead of returning a Response object when a rate limit has exceeded, it's also possible to throw an exception. This allows you to easily handle the rate limit on another level, for instance by capturing the kernel.exception event.

Running tests

If you want to run the tests use:

./vendor/bin/simple-phpunit

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言