PHPackages                             crowdstar/exponential-backoff - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. crowdstar/exponential-backoff

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

crowdstar/exponential-backoff
=============================

Prevent overloading an unavailable service by doubling the timeout each iteration.

3.0.11(3y ago)1430.2k—0%11Apache-2.0PHPPHP &gt;=7.1CI passing

Since Jun 11Pushed 1mo ago5 watchersCompare

[ Source](https://github.com/Crowdstar/exponential-backoff)[ Packagist](https://packagist.org/packages/crowdstar/exponential-backoff)[ Docs](https://www.glu.com)[ RSS](/packages/crowdstar-exponential-backoff/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (13)Used By (1)

[![Library Status](https://github.com/Crowdstar/exponential-backoff/workflows/Tests/badge.svg)](https://github.com/Crowdstar/exponential-backoff/actions)[![Latest Stable Version](https://camo.githubusercontent.com/4ef6294ddfefb28d1dafb5d3798f19a7365c09bdaa620a4db7acbf95843a7ae2/68747470733a2f2f706f7365722e707567782e6f72672f43726f7764737461722f6578706f6e656e7469616c2d6261636b6f66662f762f737461626c652e737667)](https://packagist.org/packages/crowdstar/exponential-backoff)[![Latest Unstable Version](https://camo.githubusercontent.com/8c57a647b85ad6963350e586447148adcf2df6b725d1add47548ed83776c10fc/68747470733a2f2f706f7365722e707567782e6f72672f43726f7764737461722f6578706f6e656e7469616c2d6261636b6f66662f762f756e737461626c652e737667)](https://packagist.org/packages/crowdstar/exponential-backoff)[![License](https://camo.githubusercontent.com/b572e9861f35a80dda57f12afa36803a88f92fe135742a2dec14785a97ab8d75/68747470733a2f2f706f7365722e707567782e6f72672f43726f7764737461722f6578706f6e656e7469616c2d6261636b6f66662f6c6963656e73652e737667)](https://packagist.org/packages/crowdstar/exponential-backoff)

- [Summary](#summary)
- [Installation](#installation)
- [Sample Usage](#sample-usage)
    - [1. Retry When Return Value Is Empty](#1-retry-when-return-value-is-empty)
    - [2. Retry When Certain Exceptions Thrown Out](#2-retry-when-certain-exceptions-thrown-out)
        - [Don't Throw Out an Exception When Finally Failed](#dont-throw-out-an-exception-when-finally-failed)
    - [3. Retry When Customized Condition Met](#3-retry-when-customized-condition-met)
    - [4. More Options When Doing Exponential Backoff](#4-more-options-when-doing-exponential-backoff)
    - [5. To Disable Exponential Backoff Temporarily](#5-to-disable-exponential-backoff-temporarily)
- [Sample Scripts](#sample-scripts)

Summary
=======

[](#summary)

Exponential back-offs prevent overloading an unavailable service by doubling the timeout each iteration. This class uses an exponential back-off algorithm to calculate the timeout for the next request.

This library allows doing exponential backoff in non-blocking mode in [Swoole](https://github.com/swoole/swoole-src).

Installation
============

[](#installation)

```
composer require crowdstar/exponential-backoff:~3.0.0
```

Sample Usage
============

[](#sample-usage)

In following code pieces, we assume that you want to store return value of method *MyClass::fetchData()* in variable *$result*, and you want to do exponential backoff on that because something unexpected could happen when running method *MyClass::fetchData()*.

1. Retry When Return Value Is Empty
-----------------------------------

[](#1-retry-when-return-value-is-empty)

Following code is to try to fetch some non-empty data back with method *MyClass::fetchData()*. This piece of code will try a few more times (by default 4) until either we get some non-empty data back, or we have reached maximum numbers of retries.

```

```

2. Retry When Certain Exceptions Thrown Out
-------------------------------------------

[](#2-retry-when-certain-exceptions-thrown-out)

Following code is to try to fetch some data back with method *MyClass::fetchData()*, which may throw out exceptions. This piece of code will try a few more times (by default 4) until either we get some data back, or we have reached maximum numbers of retries.

NOTE: Internal PHP errors (class [Error](https://www.php.net/error)) won't trigger exponential backoff. They should be fixed manually.

```

```

### Don't Throw Out an Exception When Finally Failed

[](#dont-throw-out-an-exception-when-finally-failed)

When method call *MyClass::fetchData()* finally fails with an exception caught, we can silence the exception without throwing it out by overriding method *AbstractRetryCondition::throwable()*:

```

```

If needed, you can have more complex logic defined when overriding method *AbstractRetryCondition::throwable()*.

3. Retry When Customized Condition Met
--------------------------------------

[](#3-retry-when-customized-condition-met)

Following code is to try to fetch some non-empty data back with method *MyClass::fetchData()*. This piece of code works the same as the first example, except that here it's implemented with a customized condition class instead of class *\\CrowdStar\\Backoff\\EmptyValueCondition*.

```

```

4. More Options When Doing Exponential Backoff
----------------------------------------------

[](#4-more-options-when-doing-exponential-backoff)

Following code is to try to fetch some data back with method *MyClass::fetchData()*. This piece of code works the same as the second example, except that here it's implemented with a customized condition class instead of class *\\CrowdStar\\Backoff\\ExceptionBasedCondition*.

In this piece of code, we also show what options are available when doing exponential backoff with the package.

```

```

5. To Disable Exponential Backoff Temporarily
---------------------------------------------

[](#5-to-disable-exponential-backoff-temporarily)

There are two ways to disable exponential backoff temporarily for code piece like following:

```

```

First, you may disable exponential backoff temporarily by calling method *\\CrowdStar\\Backoff\\ExponentialBackoff::disable()*. For example:

```

```

You may also disable exponential backoff temporarily by using class *\\CrowdStar\\Backoff\\NullCondition*:

```

```

All these 3 code piece work the same, having return value of method call *MyClass::fetchData()* assigned to variable *$result*.

Sample Scripts
==============

[](#sample-scripts)

Sample scripts can be found under folder *examples/*. Before running them under CLI, please do a composer update first:

```
composer update -n
```

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance60

Regular maintenance activity

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 98.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~162 days

Recently: every ~209 days

Total

12

Last Release

1109d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/865547?v=4)[Demin Yin](/maintainers/deminy)[@deminy](https://github.com/deminy)

---

Top Contributors

[![deminy](https://avatars.githubusercontent.com/u/865547?v=4)](https://github.com/deminy "deminy (68 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/crowdstar-exponential-backoff/health.svg)

```
[![Health](https://phpackages.com/badges/crowdstar-exponential-backoff/health.svg)](https://phpackages.com/packages/crowdstar-exponential-backoff)
```

###  Alternatives

[phpmentors/stagehand-fsm

A finite state machine

361.1k](/packages/phpmentors-stagehand-fsm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
