jbzoo/retry 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jbzoo/retry

Composer 安装命令:

composer require jbzoo/retry

包简介

Tiny PHP library providing retry functionality with multiple backoff strategies and jitter support

README 文档

README

CI Coverage Status Psalm Coverage Psalm Level CodeFactor Stable Version Total Downloads Dependents GitHub License

Tiny PHP library providing retry functionality with multiple backoff strategies and jitter support.

Features

  • 4 retry strategies (plus the ability to use your own)
  • Optional jitter/randomness to spread out retries and minimize collisions
  • Wait time cap to limit maximum retry delays
  • Custom callbacks for retry logic and error handling
  • Type-safe with strict typing and comprehensive test coverage
  • Backward compatible with stechstudio/backoff

Requirements

  • PHP 8.2 or higher

About This Fork

This library is a modernized fork of stechstudio/backoff with several improvements:

  • Strict typing and comprehensive test coverage
  • Explicit configuration instead of global static defaults
  • Better naming - "retry" terminology instead of "backoff"
  • Enhanced jitter control with setJitterPercent() and setJitterMinCap() methods
  • Backward compatibility through aliases

Installation

composer require jbzoo/retry

Quick Start

This library provides sane defaults for immediate use. By default: quadratic strategy with 100ms base time (attempt^2 * 100), maximum 5 retries, and no jitter.

Simple Function Usage

The simplest way to use Retry is with the retry helper function:

use function JBZoo\Retry\retry;

$result = retry(function() {
    return doSomeWorkThatMightFail();
});

If successful $result will contain the result of the closure. If max attempts are exceeded the inner exception is re-thrown.

You can of course provide other options via the helper method if needed.

Parameters: $callback, $maxAttempts, $strategy, $waitCap, $useJitter.

Class-Based Usage

Constructor parameters: $maxAttempts, $strategy, $waitCap, $useJitter.

use JBZoo\Retry\Retry;

$retry = new Retry(10, 'exponential', 10000, true);
$result = $retry->run(function() {
    return doSomeWorkThatMightFail();
});

Fluent Interface

For dependency injection scenarios, use chainable setters:

use JBZoo\Retry\Retry;

$result = (new Retry())
    ->setStrategy('constant')
    ->setMaxAttempts(10)
    ->enableJitter()
    ->run(function() {
        return doSomeWorkThatMightFail();
    });

Configuration Philosophy

This library enforces explicit configuration over global defaults. Unlike the original library, static configuration variables are deprecated and disabled. This design choice ensures:

  • Different parts of your project can have completely different retry settings
  • No conflicts with third-party libraries using their own defaults
  • Clear, explicit dependency injection patterns

Use dependency injection or direct instantiation instead of global configuration.

Retry Strategies

Four built-in strategies are available, each with a default base time of 100 milliseconds:

Constant Strategy

Sleeps for a fixed time on each retry.

use JBZoo\Retry\Strategies\ConstantStrategy;
$strategy = new ConstantStrategy(500); // 500ms each retry

Linear Strategy

Sleep time increases linearly: attempt × baseTime.

use JBZoo\Retry\Strategies\LinearStrategy;
$strategy = new LinearStrategy(200); // 200ms, 400ms, 600ms...

Polynomial Strategy

Sleep time follows polynomial growth: (attempt^degree) × baseTime.

use JBZoo\Retry\Strategies\PolynomialStrategy;
$strategy = new PolynomialStrategy(100, 3); // (attempt^3) × 100ms
// Default degree is 2 (quadratic): 100ms, 400ms, 900ms...

Exponential Strategy

Sleep time grows exponentially: (2^attempt) × baseTime.

use JBZoo\Retry\Strategies\ExponentialStrategy;
$strategy = new ExponentialStrategy(100); // 200ms, 400ms, 800ms...

Strategy Usage Options

String-Based Configuration

use JBZoo\Retry\Retry;
use function JBZoo\Retry\retry;

retry(fn() => doWork(), 10, 'constant'); // Uses ConstantStrategy with 100ms default
$retry = new Retry(10, 'constant');

Instance-Based Configuration

use JBZoo\Retry\Retry;
use JBZoo\Retry\Strategies\LinearStrategy;
use function JBZoo\Retry\retry;

retry(fn() => doWork(), 10, new LinearStrategy(500));
$retry = new Retry(10, new LinearStrategy(500));

Integer-Based Configuration

Passing an integer creates a ConstantStrategy with that base time:

retry(fn() => doWork(), 10, 1000); // 1000ms constant delay
$retry = new Retry(10, 1000);

Custom Closure Strategy

Define your own strategy with a closure:

// Closure receives attempt number and returns sleep time in milliseconds
retry(fn() => doWork(), 10, fn($attempt) => (100 * $attempt) + 5000);

$retry = new Retry(10);
$retry->setStrategy(fn($attempt) => (100 * $attempt) + 5000);

Wait Cap

Limit maximum wait time for fast-growing strategies (like exponential):

retry(fn() => doWork(), 10, 'exponential', 5000); // Cap at 5 seconds
$retry = new Retry()->setWaitCap(5000);

Jitter

Prevent retry collisions by adding randomness to wait times. This is crucial when multiple clients might retry simultaneously.

retry(fn() => doWork(), 10, 'exponential', null, true); // Enable jitter
$retry = new Retry()->enableJitter();

Advanced Jitter Control

Fine-tune jitter behavior with additional methods:

$retry = new Retry()
    ->enableJitter()
    ->setJitterPercent(75)    // Use 75% of calculated wait time as max
    ->setJitterMinCap(100);   // Minimum jitter time of 100ms

By default, this library uses "FullJitter" - a random time between 0 and the calculated wait time. See AWS's excellent explanation for more details.

Advanced Usage

Custom Retry Logic

Implement custom retry conditions beyond simple exception handling:

use JBZoo\Retry\Retry;

$retry = new Retry();
$retry->setDecider(function($attempt, $maxAttempts, $result, $exception = null) {
    // Custom logic: retry based on time, specific exceptions, return values, etc.
    return $attempt < 3 && ($exception instanceof SpecificException);
});

Error Handling

Add logging or monitoring for retry attempts:

use JBZoo\Retry\Retry;

$retry = new Retry();
$retry->setErrorHandler(function($exception, $attempt, $maxAttempts) {
    error_log("Retry {$attempt}/{$maxAttempts}: {$exception->getMessage()}");
});

Development

Running Tests

make update    # Install dependencies
make test      # Run PHPUnit tests
make codestyle # Run code quality checks
make test-all  # Run both tests and code style

License

MIT

See Also

  • CI-Report-Converter - Converting different error reports for deep compatibility with popular CI systems.
  • Composer-Diff - See what packages have changed after composer update.
  • Composer-Graph - Dependency graph visualization of composer.json based on mermaid-js.
  • Mermaid-PHP - Generate diagrams and flowcharts with the help of the mermaid script language.
  • Utils - Collection of useful PHP functions, mini-classes, and snippets for every day.
  • Image - Package provides object-oriented way to manipulate with images as simple as possible.
  • Data - Extended implementation of ArrayObject. Use files as config/array.
  • SimpleTypes - Converting any values and measures - money, weight, exchange rates, length, ...

jbzoo/retry 适用场景与选型建议

jbzoo/retry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 75.7k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 03 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「middleware」 「retry」 「failure」 「backoff」 「restart」 「Jitter」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jbzoo/retry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jbzoo/retry 我们能提供哪些服务?
定制开发 / 二次开发

基于 jbzoo/retry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 75.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 27
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-18