bentools/guzzle-throttle-middleware
Composer 安装命令:
composer require bentools/guzzle-throttle-middleware
包简介
A GuzzleHTTP Middleware that can delay requests before sending them.
README 文档
README
Guzzle Throttle Middleware
This middleware adds throttling capabilities to your Guzzle client.
This can be useful when some hosts limits your number of requests per second / per minute.
Installation
composer require bentools/guzzle-throttle-middleware
Counter storage
By default, request counters are stored in an array.
But you can use the PSR6Adapter to store your counters within a psr/cache implementation,
such as symfony/cache, and use shared storages like Redis, APCu, Memcached, ...
Usage
For this middleware to work, you need to register some configurations.
A configuration is composed of:
- A Request matcher (to trigger or not the throttler, depending on the request content)
- A maximum number of requests
- The period, in seconds, during which the maximum number of requests apply.
- A storage key.
You can register as many configurations as you need. The 1st request matcher wins.
Example
namespace App\RequestMatcher; use BenTools\Psr7\RequestMatcherInterface; use Psr\Http\Message\RequestInterface; class ExampleOrgRequestMatcher implements RequestMatcherInterface { /** * @inheritDoc */ public function matchRequest(RequestInterface $request) { return false !== strpos($request->getUri()->getHost(), 'example.org'); } }
use App\RequestMatcher\ExampleOrgRequestMatcher; use BenTools\GuzzleHttp\Middleware\Storage\Adapter\ArrayAdapter; use BenTools\GuzzleHttp\Middleware\ThrottleConfiguration; use BenTools\GuzzleHttp\Middleware\ThrottleMiddleware; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; require_once __DIR__ . '/vendor/autoload.php'; $stack = HandlerStack::create(); $middleware = new ThrottleMiddleware(new ArrayAdapter()); // Max 1 request per second $maxRequests = 1; $durationInSeconds = 1; $middleware->registerConfiguration( new ThrottleConfiguration(new ExampleOrgRequestMatcher(), $maxRequests, $durationInSeconds, 'example') ); $stack->push($middleware, 'throttle'); $client = new Client([ 'handler' => $stack, ]); $client->get('http://www.example.org'); // Will be executed immediately $client->get('http://www.example.org'); // Will be executed in 1 second
Tests
./vendor/bin/phpunit
Known issues
Due to PHP's synchronous behaviour, remember that throttling means calling sleep() or usleep() functions, which will delay your entire script, and not only the current request.
This means throttling will also block Guzzle's asynchronous requests when using CurlMultiHandler.
To prevent this, you may have a look at bentools/guzzle-queue-handler, a handler that delegates asynchronous requests to PHP workers (Beanstalk, RabbitMQ, Redis, ...).
You can then enable throttling only on workers.
See also
- bentools/guzzle-queue-handler - A queue handler to process Guzzle 6+ requests within a work queue.
- kevinrob/guzzle-cache-middleware - A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.
- bentools/guzzle-duration-middleware - A Guzzle 6+ Middleware that adds a X-Request-Duration header to all responses to monitor response times.
bentools/guzzle-throttle-middleware 适用场景与选型建议
bentools/guzzle-throttle-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 258.45k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2017 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bentools/guzzle-throttle-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bentools/guzzle-throttle-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 258.45k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-07-11