s-patompong/php-retrier
Composer 安装命令:
composer require s-patompong/php-retrier
包简介
PHP Retrier lets you retry your logic easily.
关键字:
README 文档
README
Retrier can help you retry your logic easily.
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; $api = new ApiConnector(); // By default, Retrier use RetryThrowableStrategy which will retry if the result is an instance of \Throwable $result = (new Retrier()) ->setLogic(function() use($api) { return $api->get(); }) ->execute();
Installation
You can install the package via composer:
composer require s-patompong/php-retrier
Usage
Fields and their default values:
| Field | Description | Setter | Default |
|---|---|---|---|
| delay | Wait time between each retry | setDelay(int $delay): Retrier | 3 |
| retryTimes | Number of retry | setRetryTimes(int $retryTimes): Retrier | 3 |
| onRetryListener | A closure that will be called on retry | setOnRetryListener(function(int $currentTryTimes, ?mixed $returnedValue, ?\Throwable $throwable): void {}): Retrier | null |
| retryStrategy | A class that implement RetryStrategy interface | setRetryStrategy(RetryStrategy $retryStrategy): Retrier | null |
Minimal configuration example:
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; $api = new ApiConnector(); $retrier = (new Retrier()) ->setLogic(function() { return 0; }); // After 3 retries, it's possible that the code still gets the \Throwable // Thus, we still need to put it in a try/catch block try { $value = $retrier->execute(); } catch(\Throwable $t) { echo "Still gets throwable after 3 retries.\n"; }
Full configuration example:
<?php // Your own API class use App\Api\ApiConnector; use SPatompong\Retrier\Retrier; use SPatompong\Retrier\Presets\Strategies\RetryNullStrategy; $api = new ApiConnector(); // Keep track of retry count, useful for logging or echoing to the terminal $retryCount = 0; $value = (new Retrier()) // Change the stragegy to RetryNullStrategy to keep retrying if the Logic returns null ->setRetryStrategy(new RetryNullStrategy()) // Set the wait time for each retry to 10 seconds ->setDelay(10) // Let the code retry 5 times ->setRetryTimes(5) // Set the onRetryListener to print out some useful log ->setOnRetryListener(function ($currentTryCount, $value, $throwable) use (&$retryCount) { $retryCount++; echo "Failed to get API data, retry count: $retryCount\n"; }) // Set the logic ->setLogic(fn () => $api->get()) // Execute it ->execute(); // At this point, value could still be null if after 5 times the code still couldn't get the API data echo $value;
It's also possible to use callable array syntax when set the logic or retryListener:
<?php use SPatompong\Retrier\Retrier; use SPatompong\Retrier\Tests\Helpers\FakeClass; $fakeClass = new FakeClass(); $publicMethodResult = (new Retrier()) ->setLogic([$fakeClass, 'fakePublicMethod']) ->execute(); $staticMethodResult = (new Retrier()) ->setLogic([FakeClass::class, 'fakeStaticMethod']) ->execute();
Retry Strategy
RetryStrategy is a class that implement RetryStrategy interface. The Retrier class uses it to determine if it should retry or not (given the return value from the logic).
<?php namespace SPatompong\Retrier\Contracts; interface RetryStrategy { /** * Add a logic to check if the retrier should retry * * @param mixed $value * @return bool */ public function shouldRetry(mixed $value): bool; }
This library provides two presets strategy:
RetryThrowableStrategy- This is a default strategy that will retry any \Throwable response.RetryNullStrategy- This strategy will keep retry if the response is NULL.
If you want to have a custom shouldRetry() logic, you can create your own RetryStrategy class and implement this RetryStrategy interface.
<?php namespace App\RetryStrategies; use SPatompong\Retrier\Contracts\RetryStrategy; use GuzzleHttp\Exception\ClientException; class RetryGuzzleClientExceptionStrategy implements RetryStrategy { public function shouldRetry(mixed $value): bool { return $value instanceof ClientException; } }
Then, set it as a retry strategy of the Retrier:
<?php use SPatompong\Retrier\Retrier; use App\RetryStrategies\RetryGuzzleClientExceptionStrategy; $retrier = (new Retrier()) ->setRetryStrategy(new RetryGuzzleClientExceptionStrategy()) try { $retrier->execute(); } catch(\Throwable $t) { // Still gets ClientException after retry or other type of Throwable }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
s-patompong/php-retrier 适用场景与选型建议
s-patompong/php-retrier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 09 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「s-patompong」 「php-retrier」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 s-patompong/php-retrier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 s-patompong/php-retrier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 s-patompong/php-retrier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-23