定制 bedrockstreaming/rate-limit-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

bedrockstreaming/rate-limit-bundle

Composer 安装命令:

composer require bedrockstreaming/rate-limit-bundle

包简介

Manage rate limitation for your routes

README 文档

README

This bundle provides an easy way to protect your project by limiting access to your controllers.

Install the bundle

composer require bedrockstreaming/rate-limit-bundle

Update your config/bundles.php file to add the bundle for all env

<?php

return [
    ...
    Bedrock\Bundle\RateLimitBundle\RateLimitBundle::class => ['all' => true],
    ...
];

Configure the bundle

Add the config/packages/bedrock_rate_limit.yaml file with the following data.

bedrock_rate_limit:
    limit: 25 # 1000 requests by default
    period: 600 # 60 seconds by default
    limit_by_route: true|false # false by default
    display_headers: true|false # false by default

By default, the limitation is common to all routes annotated #[RateLimit]. For example, if you keep the default configuration and you configure the #[RateLimit] attribute in 2 routes. Limit will shared between this 2 routes, if user consume all authorized calls on the first route, the second route couldn't be called. If you swicth limit_by_route to true, users will be allowed to reach the limit on each route annotated.

#[GraphQLRateLimit]attribute allows you to rate limit by graphQL query or mutation. /!\ To use this attribute, you will need to install suggested package.

If you switch display_headers to true, 3 headers will be added x-rate-limit, x-rate-limit-hits, x-rate-limit-untils to your responses. This can be usefull to debug your limitations. display_headers is used to display a verbose return if limit is reached.

Configure your storage

You must tell Symfony which storage implementation you want to use.

Update your config/services.yml like this:

    ...
    Bedrock\Bundle\RateLimitBundle\Storage\RateLimitStorageInterface: '@Bedrock\Bundle\RateLimitBundle\Storage\RateLimitInMemoryStorage'
    ...

By default, only RateLimitInMemory is provided. But feel free to create your own by implementing RateLimitStorageInterface or ManuallyResetableRateLimitStorageInterface. If your database has a TTL system (like Redis), you can implement only RateLimitStorageInterface. Otherwhise you must implement also ManuallyResetableRateLimitStorageInterface to manually delete rate limit in your database.

Configure your modifiers

Modifiers are a way to customize the rate limit.

This bundle provides 2 modifiers:

  • HttpMethodRateLimitModifier limits the requests by http_method.
  • RequestAttributeRateLimitModifier limits the requests by attributes value (taken from the $request->attributes Symfony's bag).

Update your config/services.yml like this:

    ...
    Bedrock\Bundle\RateLimitBundle\RateLimitModifier\HttpMethodRateLimitModifier:
        tags: [ 'rate_limit.modifiers' ]   

    Bedrock\Bundle\RateLimitBundle\RateLimitModifier\RequestAttributeRateLimitModifier:
            arguments:
                $attributeName: 'myRequestAttribute'
            tags: [ 'rate_limit.modifiers' ]
 
    ...

You can also create your own rate limit modifier by implementing RateLimitModifierInterface and tagging your service accordingly.

Configure your routes

With attribute

Add the #[RateLimit] attribute to your controller methods (by default, the limit will be 1000 requests per minute). This attribute accepts parameters to customize the rate limit. The following example shows how to limit requests on a route at the rate of 10 requests max every 2 minutes. :warning: This customization only works if the limit_by_route parameter is true

#[RateLimit(limit: 10, period: 120)]

To rate limit your graphQL API, add the #[GraphQlRateLimit] attribute to your graphQL controller. This attribute requires a list of endpoints and accepts parameters to customize the rate limit. The following example shows how to limit requests on an endpoint at the rate of 10 requests max every 2 minutes and on default limitations.

#[GraphQlRateLimit(
    endpoints: [
        [ 'endpoint' => 'GetMyQuery', 'limit' => 10, 'period' => 12],
        [ 'endpoint' => 'EditMyMutation'],
    ]
)]

In YAML

You also may add your rate limits in configuration files (Yaml) with the route name. If period or limit is not defined for a route, the bundle will take the common option.

bedrock_rate_limit:
    limit: 1000
    period: 60

    routes:
        get_foobar:
            limit: 500
            period: 10
        post_foobar:
            period: 10

Migration to php8

If you use rector you can use those rules to automatically migrate annotation to attributes :

$rectorConfig->ruleWithConfiguration(
    \Rector\Php80\Rector\Class_\AnnotationToAttributeRector::class,
    [
        new \Rector\Php80\ValueObject\AnnotationToAttribute(
        'Bedrock\Bundle\RateLimitBundle\Annotation\GraphQLRateLimit',
        \Bedrock\Bundle\RateLimitBundle\Attribute\GraphQLRateLimit::class
        ),
        new \Rector\Php80\ValueObject\AnnotationToAttribute(
            'Bedrock\Bundle\RateLimitBundle\Annotation\RateLimit',
            \Bedrock\Bundle\RateLimitBundle\Attribute\RateLimit::class
        )
    ],
);

bedrockstreaming/rate-limit-bundle 适用场景与选型建议

bedrockstreaming/rate-limit-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.35k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 11 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 bedrockstreaming/rate-limit-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 bedrockstreaming/rate-limit-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 bedrockstreaming/rate-limit-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 24.35k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 18
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 33
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-18