guzzlehttp/retry-subscriber
最新稳定版本:2.0.2
Composer 安装命令:
composer require guzzlehttp/retry-subscriber
包简介
Retries failed HTTP requests using customizable retry strategies (Guzzle 4+)
README 文档
README
Retries failed HTTP requests using customizable retry strategies.
Here's a simple example of how it's used:
use GuzzleHttp\Subscriber\Retry\RetrySubscriber; // Retry 500 and 503 responses $retry = new RetrySubscriber([ 'filter' => RetrySubscriber::createStatusFilter() ]); $client = new GuzzleHttp\Client(); $client->getEmitter()->attach($retry);
Installing
Add the following to your composer.json:
{ "require": { "guzzlehttp/retry-subscriber": "~2.0" } } Creating a RetrySubscriber
The constructor of the RetrySubscriber accepts an associative array of configuration options:
- filter
- (callable) (Required) Filter used to determine whether or not to retry a request. The filter must be a callable that accepts the current number of retries as the first argument and a
GuzzleHttp\Event\AbstractTransferEventobject as the second argument. The filter must returntrueorfalseto denote if the request must be retried. - delay
Callable that accepts the number of retries as the first argument and a
GuzzleHttp\Event\AbstractTransferEventas the second argument. The callable must return the amount of of time (in milliseconds) to delay.If no
delayconfiguration value is provided, then a default exponential backoff implementation is used.- max
Integer representing the maximum number of retries to allow before giving up.
If no
maxconfiguration value is provided, then a request will be retried 5 times.
Determining what should be retried
The required filter option of the RetrySubscriber's constructor is a callable that is invoked to determine if a request should be retried.
When the filter is invoked, it is provided the current retry count for the request and a GuzzleHttp\Event\CompleteEvent or GuzzleHttp\Event\ErrorEvent (both events extend from GuzzleHttp\Event\AbstractTransferEvent, so you should typehint on that). The filter must then return true if the request should be retried, or false if it should not be retried.
Here's an example of retrying failed 500 responses sent to the /foo endpoint:
use GuzzleHttp\Event\AbstractTransferEvent; use GuzzleHttp\Subscriber\Retry\RetrySubscriber; $retry = new RetrySubscriber([ 'filter' => function ($retries, AbstractTransferEvent $event) { $resource = $event->getRequest()->getResource(); // A response is not always received (e.g., for timeouts) $code = $event->getResponse() ? $event->getResponse()->getStatusCode() : null; return $resource == '/foo' && $code == 500; } ]); $client = new GuzzleHttp\Client(); $client->getEmitter()->attach($retry);
Filter Chains
You can create more customizable retry logic with filter chains, which are created using the static RetrySubscriber::createChainFilter() method. This method accepts an array of callable filters that are each invoked one after the other. The filters in the chain should return one of the following values, which affects how the rest of the chain is executed.
RetrySubscriber::RETRY(i.e.,true) – Retry the request.RetrySubscriber::DEFER(i.e.,false) – Defer to the next filter in the chain.RetrySubscriber::BREAK_CHAIN(i.e.,-1) – Stop the filter chain, and do not retry the request.
Here's an example using filter chains that retries failed 500 and 503 responses for only idempotent or "safe" requests as defined by RFC 7231.
use GuzzleHttp\Event\AbstractTransferEvent; use GuzzleHttp\Subscriber\Retry\RetrySubscriber; // Retry 500 and 503 responses that were sent as GET and HEAD requests. $filter = RetrySubscriber::createChainFilter([ // Does early filter to force non-idempotent methods to NOT be retried. RetrySubscriber::createIdempotentFilter(), // Performs the last check, returning ``true`` or ``false`` based on // if the response received a 500 or 503 status code. RetrySubscriber::createStatusFilter([500, 503]) ]); $retry = new RetrySubscriber(['filter' => $filter]); $client = new GuzzleHttp\Client(); $client->getEmitter()->attach($retry);
Customizing the amount of delay
delay is an optional configuration option in the RetrySubscriber's constructor that is a callable used to determine the amount of time to delay before retrying a request that has been marked as needing a retry. The callable accepts the current number of retries and either a GuzzleHttp\Event\CompleteEvent or a GuzzleHttp\Event\ErrorEvent. The function must then return an integer or float representing the amount of time in milliseconds to sleep.
Note
Omitting this argument will use a default exponential backoff strategy.
Here's an example of creating a custom delay that always delays for 1 millisecond:
use GuzzleHttp\Subscriber\Retry\RetrySubscriber; $retry = new RetrySubscriber([ 'filter' => RetrySubscriber::createStatusFilter(), 'delay' => function ($number, $event) { return 1; } ]);
Changing the max number of retries
You can also specify an optional max number of retries in the max configuration option of the RetrySubscriber's constructor. If not specified, a request can be retried up to 5 times before it is allowed to fail.
use GuzzleHttp\Subscriber\Retry\RetrySubscriber; $retry = new RetrySubscriber([ 'filter' => RetrySubscriber::createStatusFilter(), 'max' => 3 ]);
guzzlehttp/retry-subscriber 适用场景与选型建议
guzzlehttp/retry-subscriber 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 973.69k 次下载、GitHub Stars 达 58, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「http」 「Guzzle」 「retry」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 guzzlehttp/retry-subscriber 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 guzzlehttp/retry-subscriber 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 guzzlehttp/retry-subscriber 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Emulated timeouts for synchronous operations.
Retry tasks that fail due to transient faults
Polling library for conditionally retry operations based on a result checker.
Retry policy plugin for the Testo testing framework.
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 973.69k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 58
- 点击次数: 32
- 依赖项目数: 20
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04