eventsauce/backoff
Composer 安装命令:
composer require eventsauce/backoff
包简介
Back-off strategy interface
README 文档
README
This library provides an interface for encapsulated back-off strategies.
composer require eventsauce/backoff
Leveraging the back-off strategies
A back-off strategy is applied in side a piece of code that retries a certain task.
<?php use EventSauce\BackOff\BackOffStrategy; class BusinessLogic { public function __construct( private ExternalDependency $dependency, private BackOffStrategy $backOff, ) {} public function performAction(): void { $tries = 0; start: try { ++$tries; $this->dependency->actionThatMayFail(); } catch (Throwable $throwable) { $this->backOff->backOff($tries, $throwable); goto start; } } }
Exponential back-off
A well-known back-off strategy is exponential back-off, which is the default provided strategy.
sleep = initial_delay * (base ^ (number_of_tries - 1)
<?php use EventSauce\BackOff\ExponentialBackOffStrategy; $backOff = new ExponentialBackOffStrategy( 100000, // initial delay in microseconds, 0.1 seconds 15, // max number of tries 2500000, // (optional) max delay in microseconds, default 2.5 seconds 2.0, // (optional) base to control the growth factor, default 2.0 ); $businessLogic = new BusinessLogic(new ExternalDependency(), $backOff); try { $businessLogic->performAction(); } catch (Throwable $throwable) { // handle the throwable }
Fibonacci back-off
The Fibonacci back-off strategy increases the back-off based on the fibonacci sequence.
sleep = initial_delay * fibonacci(number_of_tries)
<?php use EventSauce\BackOff\FibonacciBackOffStrategy; $backOff = new FibonacciBackOffStrategy( 100000, // initial delay in microseconds, 0.1 seconds 15, // max number of tries 2500000, // (optional) max delay in microseconds, default 2.5 seconds ); $businessLogic = new BusinessLogic(new ExternalDependency(), $backOff); try { $businessLogic->performAction(); } catch (Throwable $throwable) { // handle the throwable }
Linear back-off
The linear back-off strategy increases the back-off time linearly.
sleep = initial_delay * number_of_tries
<?php use EventSauce\BackOff\LinearBackOffStrategy; $backOff = new LinearBackOffStrategy( 100000, // initial delay in microseconds, 0.1 seconds 15, // max number of tries 2500000, // (optional) max delay in microseconds, default 2.5 seconds ); $businessLogic = new BusinessLogic(new ExternalDependency(), $backOff); try { $businessLogic->performAction(); } catch (Throwable $throwable) { // handle the throwable }
Jitter
When many clients are forced to retry, having deterministic interval can cause many of these clients to retry at the same time. Adding randomness to the mix ensures retrying clients are scattered across time. The randomness ensures that it is less likely for the clients to all retry at the same time.
Using Jitter
Every strategy that sleeps accepted a EventSauce\BackOff\Jitter\Jitter
implementation.
use EventSauce\BackOff\ExponentialBackOffStrategy; use EventSauce\BackOff\FibonacciBackOffStrategy; use EventSauce\BackOff\LinearBackOffStrategy; $exponential = new ExponentialBackOffStrategy(100000, 25, jitter: $jitter); $fibonacci = new FibonacciBackOffStrategy(100000, 25, jitter: $jitter); $linear = new LinearBackOffStrategy(100000, 25, jitter: $jitter);
Full Jitter
The full jitter uses a randomized value from 0 to the initial calculated sleep time.
sleep = number_between(0, sleep)
use EventSauce\BackOff\Jitter\FullJitter; $jitter = new FullJitter();
Half Jitter
The full jitter uses a randomized value from half the initial sleep to the full initial sleep time.
sleep = sleep / 2 + number_between(0 , sleep / 2)
use EventSauce\BackOff\Jitter\HalfJitter; $jitter = new HalfJitter();
Scattered Jitter
The scattered jitter uses a range in across which it's scatter the resulting values. To illustrate, here are a few examples:
| Range | Min | Max |
|---|---|---|
| 0.25 | 75% | 125% |
| 0.5 | 50% | 150% |
| 0.1 | 90% | 110% |
jittered = sleep * range
base = sleep - jittered
sleep = base + number_between(0 , jittered * 2)
use EventSauce\BackOff\Jitter\ScatteredJitter; $jitter = new ScatteredJitter($range = 0.5);
Design rationale
Unlike other exponential back-off libraries, this library doesn't run the operation you want to retry. This makes the design of the package very simple. It also doesn't impose any limitations on the surround code.
You can retry based on a return value:
use EventSauce\BackOff\BackOffStrategy; function action(Client $client, BackOffStrategy $backOff): void { $tries = 0; start: $tries++; $response = $client->doSomething(); if ($response == SomeParticular::VALUE) { $backOff->backOff($tries, new LogicException('Exhausted back-off')); goto start; } }
You can retry on a specific exception type:
use EventSauce\BackOff\BackOffStrategy; function action(Client $client, BackOffStrategy $backOff): void { $tries = 0; start: $tries++; try { $client->doSomething(); } catch (SpecificException $exception) { $backOff->backOff($tries, $exception); goto start; } }
The choice is yours. Enjoy!
PS: yes, those were a lot of goto statements, deal with it 😎
But Frank, I'm super lazy!
Ok ok ok, well in that case, use the BackOffRunner class to run any
callable with a retry strategy.
use EventSauce\BackOff\BackOffRunner; use EventSauce\BackOff\ExponentialBackOffStrategy; $strategy = new ExponentialBackOffStrategy(initialDelayMs: 100, maxTries: 5); $runner = new BackOffRunner($strategy); $runner->run(function () { // Do something that might throw an exception! });
Want to only retry on certain exceptions? Pass the exception class as the second constructor argument.
use EventSauce\BackOff\BackOffRunner; use EventSauce\BackOff\ExponentialBackOffStrategy; $strategy = new ExponentialBackOffStrategy(initialDelayMs: 100, maxTries: 5); $runner = new BackOffRunner($strategy, LogicException::class); $runner->run(function () { // Only LogicException is retried, the rest is not! });
eventsauce/backoff 适用场景与选型建议
eventsauce/backoff 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 843.53k 次下载、GitHub Stars 达 70, 最近一次更新时间为 2021 年 04 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「retry」 「backoff」 「Jitter」 「exponential」 「back-off」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eventsauce/backoff 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eventsauce/backoff 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eventsauce/backoff 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple backoff / retry functionality
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.
统计信息
- 总下载量: 843.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 71
- 点击次数: 28
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-04-15