ryudith/mezzio-simple-throttle

Mezzio 的简单限流中间件。

v1.0.1 2022-06-07 06:56 UTC

This package is auto-updated.

Last update: 2024-09-10 12:37:21 UTC


README

Ryudith\MezzioSimpleThrottle 是 Mezzio 框架的限流中间件。

安装

要安装,请运行以下命令

$ composer require ryudith/mezzio-simple-throttle

用法

首先将 ConfigProvider::class 添加到 config/config.php

...

$aggregator = new ConfigAggregator([
    ...

    \Ryudith\MezzioSimpleThrottle\ConfigProvider::class,  // <= add this line
    
    ...

    class_exists(\Mezzio\Swoole\ConfigProvider::class)
        ? \Mezzio\Swoole\ConfigProvider::class
        : function (): array {
            return [];
        },
    ...
], $cacheConfig['config_cache_path']);

...

然后通过将 SimpleThrottle::class 添加到 config/pipeline.php 注册中间件

...

use Ryudith\MezzioSimpleThrottle\SimpleThrottle;  // <= add this line

...

return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
    // The error handler should be the first (most outer) middleware to catch
    // all Exceptions.
    $app->pipe(ErrorHandler::class);
    $app->pipe(ServerUrlMiddleware::class);
    $app->pipe(SimpleThrottle::class);  // <= add this line

    ...

};

...

如果您想的话,可以在 $app->pipe(ErrorHandler::class) 之前添加 $app->pipe(SimpleThrottle::class)

自定义配置

中间件的配置位于 vendor/ryudith/mezzio-simple-throttle/ConfigProvider.php,其中重要内容为

...

return [
    'dependencies' => [
        'factories' => [
            FileSystemThrottleStorage::class => FileSystemThrottleStorageFactory::class,
            ThrottleResponse::class => ThrottleResponseFactory::class,
            SimpleThrottle::class => SimpleThrottleFactory::class,
        ],
    ],
    'mezzio_simple_throttle' => [
        'request_limit_per_minute' => 10,
        'request_real_ip_key' => 'REMOTE_ADDR',  // key for $_ENV or $_SERVER to get request real ip
        'ip_path_key' => true,  // data key based IP and URI path or IP only data key
        'throttle_data_dir' => './data/throttle',
        'file_data_delimiter' => '||',
        'throttle_storage_class' => FileSystemThrottleStorage::class,
        'throttle_response_class' => ThrottleResponse::class,
    ],
];

...

详细说明

  1. request_limit_per_minute 表示触发限流前的请求数量限制

  2. request_real_ip_key 是获取请求 IP 的关联键,默认为 'REMOTE_ADDR',如果您在您的 web 服务器上有自定义键,可以更改它。

  3. ip_path_key 是一个标志,用于生成 IP-Path 组合的键或仅 IP 地址。

  4. throttle_data_dir 是用于保存限流记录数据的字符串路径。

  5. file_data_delimiter 是文件内部的数据分隔符,因为这个库基于文件记录数据。

  6. throttle_storage_class 是用于保存限流数据的服务键。如果您想更改存储类型,可以更改它(不要忘记在您的自定义类上实现接口 Ryudith\MezzioSimpleThrottle\Storage\StorageInterface)。

  7. throttle_response_class 是在达到限流限制时提供响应的类。如果您想使用自己的类,也可以更改它(不要忘记在您的自定义类上实现接口 Ryudith\MezzioSimpleThrottle\Response\ThrottleResponseInterface)。

您不需要直接编辑 ConfigProvider.php 内部的配置来更改配置,只需将您要更改的配置添加到 config/autoload/mezzio.global.php 或您使用的任何配置文件中即可。

例如,您可以在 config/autoload/mezzio.global.php 中添加以下自定义配置

...

return [
    // Toggle the configuration cache. Set this to boolean false, or remove the
    // directive, to disable configuration caching. Toggling development mode
    // will also disable it by default; clear the configuration cache using
    // `composer clear-config-cache`.
    ConfigAggregator::ENABLE_CACHE => true,

    // Enable debugging; typically used to provide debugging information within templates.
    'debug'  => true,
    'mezzio' => [
        // Provide templates for the error handling middleware to use when
        // generating responses.
        'error_handler' => [
            'template_404'   => 'error::4042',
            'template_error' => 'error::error',
        ],
    ],
    // add only configuration you want to change
    'mezzio_simple_throttle' => [
        'ip_path_key' => false,
        'file_data_delimiter' => '//'
    ],
];

...

文档

API 文档

问题或疑问

下一页

  • 添加排除路径和/或 IP