stn/rate-limiting-bundle

用于管理请求速率限制的包

安装: 71

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 2

开放问题: 0

类型:symfony-bundle

dev-master 2016-07-06 03:47 UTC

This package is not auto-updated.

Last update: 2024-09-23 12:02:51 UTC


README

此包(需要 Predis)为Symfony添加了对请求速率限制管理的支持。

Build Status Images

安装

使用Composer下载StnRateLimitingBundle包

$ composer require stn/rate-limiting-bundle

注册StnRateLimitingBundle

<?php
// app/AppKernel.php

// ...
    public function registerBundles()
    {
        $bundles = array(
            // Other bundles ...

            new Stn\RateLimitingBundle\StnRateLimitingBundle(),
        );
    }
// ...

配置

# app/config/config.yml
stn_rate_limiting:
    enable: true    # Whether rate limiting is available or not
    limit:  60      # Limit of request
    ttl:    60      # Cache expiry time, second as unit (Time to live)
    key_prefix: ~   # The cache key prefix, defaults to 'RL'
    key_length: ~   # The cache key length, defaults to 8
    client:         # Configuration for Predis
        dsn:  ~     # DSN for connection, defaults to 'tcp://127.0.0.1:6379'
        pass: ~     # Redis requirepass configuration, will invoke `auth` to setup connection if provided, defaults to null

用法

使用注解 @RateLimiting 来限制请求速率,您可以通过手动设置以下两个参数或使用默认配置来设置速率限制

  • 限制
  • 超时时间
// any controller
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Stn\RateLimitingBundle\Annotation\RateLimiting;

/**
 * Mark with annotation `RateLimiting` to enable rate limiting.
 *
 * @RateLimiting(limit=10, ttl=60)
 */
class DefaultController extends Controller
{
    /**
     * Annotation in controller's action has a higher priority than controller itself.
     *
     * @RateLimiting(limit=2, ttl=10)
     */
    public function indexAction()
    {
        // Do something...
    }
}

待办事项

  • 添加禁止响应模板
  • 使redis配置更加灵活
  • 更多测试

许可证

MIT