yriveiro/php-backoff
Composer 安装命令:
composer require yriveiro/php-backoff
包简介
Simple backoff / retry functionality
README 文档
README
NOTE: to use php-backoff with PHP 5.x please use the lastet release of branch 1.x
API
getDefaultOptions():
This method is static and returns an array with the default options:
cap: Max duration allowed (in microseconds). If backoff duration is greater than cap, cap is returned, default is1000000microseconds.maxAttempts: Number of attempts before thrown an Yriveiro\Backoff\BackoffException. Default is0, no limit.
exponential($attempt):
This method use and exponential function E(attempt) = (2**attempt - 1) to calculate backoff time.
Parameters
attempt: incremental value that represents the current retry number.
equalJitter($attempt);
Exponential backoff has one disadvantage. In high concurrence, we can have multiples calls with the same backoff time due the time is highly bound to the current attempt, different calls could be in the same attempt.
To solve this we can add a jitter value to allow some randomization.
equalJitter uses the function: E(attempt) = min(((2**attempt - 1) / 2), random(0, ((2**attempt - 1) / 2))).
Parameters
attempt: incremental value that represents the current retry number.
fullJitter($attempt);
Full jitter behaves like equalJitter method, the main difference between them is the way in how the jitter value is calculated.
fullJitter uses the function: E(attempt) = min(random(0, (2**attempt - 1) / 2)).
Parameters
attempt: incremental value that represents the current retry number.
Usage
Zero configuration examples:
With zero configuration, we will never stop to try fetch data. The exit condition is your responsibility.
$attempt = 1; $backoff = new Backoff(); $response = $http->get('http://myservice.com/user/1'); while (!$response) { $time = $backoff->exponential($attempt); $attempt++; usleep($time); $response = $http->get('http://myservice.com/user/1'); }
With configuration examples:
$attempt = 1; $options = Backoff::getDefaultOptions(); $options['maxAttempts'] = 3; $backoff = new Backoff($options); $response = $http->get('http://myservice.com/user/1'); try while (!$response) { $time = $backoff->fullJitter($attempt); $attempt++; usleep($time); $response = $http->get('http://myservice.com/user/1'); } } catch (Yriveiro\Backoff\BackoffException $e) { // Handle the exception }
Installation
The recommended way to install this package is through Composer.
php composer.phar require "yriveiro/php-backoff"
Tests
Tests are performed using the phpunit library, to run them:
php vendor/bin/phpunit tests
Know issues
None.
How to contribute
Have an idea? Found a bug?, contributions are welcome :)
License
Backoff is licensed under MIT license.
yriveiro/php-backoff 适用场景与选型建议
yriveiro/php-backoff 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 80.6k 次下载、GitHub Stars 达 26, 最近一次更新时间为 2016 年 01 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「retry」 「backoff」 「Jitter」 「exponential backoff」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yriveiro/php-backoff 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yriveiro/php-backoff 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yriveiro/php-backoff 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Back-off strategy interface
Tiny PHP library providing retry functionality with multiple backoff strategies and jitter support
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.
统计信息
- 总下载量: 80.6k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 27
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-01-14