nevsnode/backoff

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple PHP Backoff Library

Maintainers

Package info

github.com/nevsnode/backoff

pkg:composer/nevsnode/backoff

Statistics

Installs: 77 657

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.3.0 2018-07-11 08:28 UTC

This package is not auto-updated.

Last update: 2026-02-12 22:23:17 UTC


README

Very simple Backoff PHP library. It provides an easy solution to wait after failures with an increasing delay.

Install

composer require nevsnode/backoff

Example Usage

<?php
use nevsnode\Backoff;

$backoff = new Backoff();

$resource = new ExampleResource();
$result = $backoff->retryOnException(5, function () use ($resource) {
    $return = $resource->fetchSomething();
    if (!$return) {
        throw new Exception('Failed to fetch something');
    }
    return $return;
});

By default, when exceeding the number of retries, the last exception will just be thrown again. Optionally a final closure/value can be defined which is executed or returned instead:

<?php
$backoff = new nevsnode\Backoff();

// $result will now become the string "Some error"
$result = $backoff->retryOnException(3, function () {
    throw new \Exception('Some error');
}, function ($e) {
    return $e->getMessage();
});

// $result will now become FALSE
$result = $backoff->retryOnException(2, function () {
    throw new \Exception('Some error');
}, false);

Settings

Setting Type Default Description
min Integer 1000 Minimum delay (in ms)
max Integer 30000 Maximum delay (in ms)
factor Float 2.0 Multiplicator of delay on additional delays
jitter Boolean true Allow jitter
jitterMax Integer 2000 Maximum jitter (in ms)
exceptions List of Strings every Throwable/Exception Specific list of exception-classes which will be retried

The settings can be passed as an associative array to the constructor and returned or adjusted after instantiation:

<?php

// pass settings to constructor
$backoff = new Backoff([
    'min' => 2000,
    'max' => 10000,
    'factor' => M_E,
]);

// define setting through setter
$backoff->setJitter(true);
$backoff->setJitterMax(6000);
$backoff->setMin(3000);

// return setting through getter
$max = $backoff->getMax();