PHPackages                             koschos/php-retry - 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. koschos/php-retry

ActiveLibrary

koschos/php-retry
=================

PHP retry library which is a port of the spring retry

v2.0.0(7y ago)24MITPHPPHP ~7.2

Since Dec 20Pushed 4y ago1 watchersCompare

[ Source](https://github.com/koschos/php-retry)[ Packagist](https://packagist.org/packages/koschos/php-retry)[ RSS](/packages/koschos-php-retry/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (5)Used By (0)

[![Build Status](https://camo.githubusercontent.com/55b2f3b6e3c1c0ee905a5e3c002f061db868cea9e0bdeb2b9cddaa501281fbea/68747470733a2f2f7472617669732d63692e636f6d2f6b6f7363686f732f7068702d72657472792e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/koschos/php-retry)

Overview
========

[](#overview)

PHP library for doing retries. This is port of spring retry

Not ported
==========

[](#not-ported)

- annotations support
- stateful retry, retry state, retry cache
- retry context parameter bag
- retry listeners
- some policies

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

[](#installation)

Install package as composer dependency.

```
composer require koschos/php-retry
```

Quick start
===========

[](#quick-start)

Example

You just need to create retry template, which you should is your retry configuration.

```
// Build retry template with 5 retries and 100 milliseconds waits between them
$retryTemplate = RetryTemplateBuilder::getBuilder()
    ->withMaxAttempts(5)
    ->withBackOffPeriod(100)
    ->build();

// And run your code placed inside callback
$result = $retryTemplate->execute(new class implements RetryCallback {
  public function doWithRetry(RetryContext $context) {
      // dangerous code
  }
});
```

Features and API
================

[](#features-and-api)

RetryTemplate
-------------

[](#retrytemplate)

To make processing more robust and less prone to failure, sometimes it helps to automatically retry a failed operation in case it might succeed on a subsequent attempt. Errors that are susceptible to this kind of treatment are transient in nature. The RetryOperations interface looks like this

```
interface RetryOperations
{
    public function execute(RetryCallback $retryCallback);

    public function executeWithRecovery(RetryCallback $retryCallback, RecoveryCallback $recoveryCallback);
}
```

The basic callback is a simple interface that allows you to insert some business logic to be retried:

```
interface RetryCallback
{
    public function doWithRetry(RetryContext $retryContext);
}
```

The callback is executed and if it fails (by throwing an Exception), it will be retried until either it is successful, or the implementation decides to abort. There are two execute methods in the RetryOperations interface dealing with callback and recovery when all retry attempts are exhausted.

The simplest general purpose implementation of RetryOperations is RetryTemplate. It could be used like this

```
$template = new RetryTemplate();

// Retry policy with 30 seconds timeout
$policy = new TimeoutRetryPolicy();
$policy->setTimeout(30000);
$template->setRetryPolicy($policy);

$result = $template->execute($myRetryCallback);
```

In the example we execute a web service call and return the result to the user. If that call fails then it is retried until a timeout is reached.

RetryContext
------------

[](#retrycontext)

The method parameter for the `RetryCallback` is a `RetryContext`. Many callbacks will simply ignore the context, but if necessary it can be used to store data for the duration of the iteration.

RecoveryCallback
----------------

[](#recoverycallback)

When a retry is exhausted the RetryOperations can pass control to a different callback, the RecoveryCallback. To use this feature clients have to implement RecoveryCallback interface and call executeWithRecovery method, for example:

```
$template->executeWithRecovery($myRetryCallback, $myRecoveryCallback);
```

Retry Policies
--------------

[](#retry-policies)

Inside a RetryTemplate the decision to retry or fail in the execute method is determined by a `RetryPolicy` which is also a factory for the RetryContext. The RetryTemplate has the responsibility to use the current policy to create a RetryContext and pass that in to the RetryCallback at every attempt. After a callback fails the `RetryTemplate` has to make a call to the RetryPolicy to ask it to update its state (which will be stored in the RetryContext), and then it asks the policy if another attempt can be made. If another attempt cannot be made (e.g. a limit is reached or a timeout is detected) then the policy is also responsible for identifying the exhausted state, but not for handling the exception. The RetryTemplate will throw the original exception, when no recover is available.

```
interface RetryPolicy
{
    public function canRetry(RetryContext $context);

    public function open();

    public function close(RetryContext $context);

    public function registerException(RetryContext $context, \Exception $exception);
}
```

Retry provides some simple general purpose implementations of stateless RetryPolicy, for example a `SimpleRetryPolicy`, and the `TimeoutRetryPolicy` used in the example above.

The `SimpleRetryPolicy` just allows a retry on any of a named list of exception types, up to a fixed number of times:

```
$policy = new SimpleRetryPolicy(5, [\Exception.class, true]);
```

Retry provides next retry policies:

- SimpleRetryPolicy
- TimeoutRetryPolicy
- AlwaysRetryPolicy
- NeverRetryPolicy

BackOff Policies
----------------

[](#backoff-policies)

When retrying after a transient failure it often helps to wait a bit before trying again, because usually the failure is caused by some problem that will only be resolved by waiting. If a `RetryCallback` fails, the `RetryTemplate` can pause execution according to the `BackOffPolicy` in place.

```
interface BackOffPolicy
{
    public function start(RetryContext $context);

    public function backOff(RetryContext $context);
}
```

Retry provides next backoff policies:

- NoBackOffPolicy
- FixedBackOffPolicy

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

Every ~1 days

Total

3

Last Release

2700d ago

Major Versions

v1.0.1 → v2.0.02018-12-23

PHP version history (3 changes)v1.0.0PHP ~5.5

v1.0.1PHP ~5.6

v2.0.0PHP ~7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/f2b16d67d3a45d809e459983120d9bcb1f4c5db4b898c954ad8ea4c97b9cfa53?d=identicon)[koschos](/maintainers/koschos)

---

Top Contributors

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

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/koschos-php-retry/health.svg)

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

PHPackages © 2026

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