PHPackages                             enl/retry-loop - 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. enl/retry-loop

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

enl/retry-loop
==============

Retry Loop is tiny class for widely used concept of retry on failure code

1.0.0(8y ago)113MITPHPPHP ~7.0

Since Sep 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/enl/retry-loop)[ Packagist](https://packagist.org/packages/enl/retry-loop)[ RSS](/packages/enl-retry-loop/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

RetryLoop
=========

[](#retryloop)

Retry Loop is widely used concept which can be easily explained with "retry on failure and give up after several retries".

Installation
------------

[](#installation)

```
composer require enl/retry-loop
```

Usage
-----

[](#usage)

For example, we need to push some data into remote service with the following pseudocode:

```
$response = $serviceClient->push($uri, $data);
```

But what if this remove service is temporarily down or network connection is unstable in this particular moment?

The common solution is to use `try-catch`. But what about several retries?

This code looks much better and does the trick:

```
$loop = new RetryLoop($retries = 5);
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});
```

Give up on some exceptions
--------------------------

[](#give-up-on-some-exceptions)

Sometimes, you need to give up on several exceptions, for example, if your client throws 'BadRequestException', you should give up trying to push the data. In order to achieve this, you can specify second parameter for `RetryLoop` constructor:

```
$loop = new RetryLoop($retries = 5, $giveUpAt = [BadRequestException::class]);
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});
```

In case of `BadRequestException`, `RetryLoop` will throw `LoopFailed` exception with actual exception as previous.

Before Retry hook
-----------------

[](#before-retry-hook)

If you need to perform some action before retry (logging, reconnection, whatever you need), just use `$beforeRetry` parameter:

```
$loop = new RetryLoop($retries = 5, $giveUpAt = [BadRequestException::class], function($e) {
    $this->log('info', 'Exception caught by retry loop, retrying: '.$e->getMessage());
});
$response = $loop->run(function() use ($serviceClient, $uri, $data) {
    return $serviceClient->push($uri, $data);
});
```

RetryLoop is immutable!
-----------------------

[](#retryloop-is-immutable)

RetryLoop class itself is immutable and does not store any internal state except given constructor parameters, so that you can easily reuse single loop instance for several retries, if it is necessary

### Builder

[](#builder)

Moreover, there is a `LoopBuilder` that provides fluent interface for loop building:

```
$loop = RetryLoop::builder
    ->retries(5)
    ->giveUpAt(BadRequestException::class)
    ->giveUpAt(SomeOtherException::class)
    ->giveUpAt([YetAnotherException::class])
    ->beforeRetry(function() { sleep(5); })
    ->get(); // or just `run` and get result if builder is not needed after that.
```

###  Health Score

26

—

LowBetter than 40% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

3235d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/50587ee520f9c618e4663b3e62d04bace5458d30d8644c71a1e510ed94075e1a?d=identicon)[enl](/maintainers/enl)

---

Top Contributors

[![enl](https://avatars.githubusercontent.com/u/670322?v=4)](https://github.com/enl "enl (4 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/enl-retry-loop/health.svg)

```
[![Health](https://phpackages.com/badges/enl-retry-loop/health.svg)](https://phpackages.com/packages/enl-retry-loop)
```

###  Alternatives

[lackhurt/laravel-apollo

Apollo agent for laravel.

135.1k](/packages/lackhurt-laravel-apollo)

PHPackages © 2026

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