Download the PHP package jbzoo/retry without Composer

On this page you can find all versions of the php package jbzoo/retry. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package retry

JBZoo / Retry

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

  1. 4 retry strategies (plus the ability to use your own)
  2. Optional jitter / randomness to spread out retries and minimize collisions
  3. Wait time cap
  4. Callbacks for custom retry logic or error handling

Notes:

Installation

Defaults

This library provides sane defaults, so you can hopefully just jump in for most of your use cases.

By default, the retry is quadratic with a 100ms base time (attempt^2 * 100), a max of 5 retries, and no jitter.

Quickstart

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

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.

Method parameters are $callback, $maxAttempts, $strategy, $waitCap, $useJitter.

Retry class usage

The Retry class constructor parameters are $maxAttempts, $strategy, $waitCap, $useJitter.

Or if you are injecting the Retry class with a dependency container, you can set it up with setters after the fact. Note that setters are chainable.

Changing defaults

Important Note: It's a fork. So I left it here just for backward compatibility. Static variables are deprecated and don't work at all!

This is terrible practice! Explicit is better than implicit. ;)

So the next variables are deprecated, and they don't influence anything.

Just use dependencies injection or so and don't warm your head.

Strategies

There are four built-in strategies available: constant, linear, polynomial, and exponential.

The default base time for all strategies is 100 milliseconds.

Constant

This strategy will sleep for 500 milliseconds on each retry loop.

Linear

This strategy will sleep for attempt * baseTime, providing linear retry starting at 200 milliseconds.

Polynomial

This strategy will sleep for (attempt^degree) * baseTime, so in this case (attempt^3) * 100.

The default degree if none provided is 2, effectively quadratic time.

Exponential

This strategy will sleep for (2^attempt) * baseTime.

Specifying strategy

In our earlier code examples we specified the strategy as a string:

This would use the ConstantStrategy with defaults, effectively giving you a 100 millisecond sleep time.

You can create the strategy instance yourself in order to modify these defaults:

You can also pass in an integer as the strategy, will translate to a ConstantStrategy with the integer as the base time in milliseconds:

Finally, you can pass in a closure as the strategy if you wish. This closure should receive an integer attempt and return a sleep time in milliseconds.

Wait cap

You may want to use a fast growing retry time (like exponential) but then also set a max wait time so that it levels out after a while.

This cap can be provided as the fourth argument to the retry helper function, or using the setWaitCap() method on the Retry class.

Jitter

If you have a lot of clients starting a job at the same time and encountering failures, any of the above retry strategies could mean the workers continue to collide at each retry.

The solution for this is to add randomness. See here for a good explanation:

https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter

You can enable jitter by passing true in as the fifth argument to the retry helper function, or by using the enableJitter() method on the Retry class.

By default, we use the "FullJitter" approach outlined in the above article, where a random number between 0 and the sleep time provided by your selected strategy is used.

But you can change the maximum time for Jitter with method setJitterPercent(). It's 100 by default. Also you can set min value for jitter withsetJitterMinCap(it's0` by default).

Custom retry decider

By default, Retry will retry if an exception is encountered, and if it has not yet hit max retries.

You may provide your own retry decider for more advanced use cases. Perhaps you want to retry based on time rather than number of retries, or perhaps there are scenarios where you would want retry even when an exception was not encountered.

Provide the decider as a callback, or an instance of a class with an __invoke method. Retry will hand it four parameters: the current attempt, max attempts, the last result received, and the exception if one was encountered. Your decider needs to return true or false.

Error handler callback

You can provide a custom error handler to be notified anytime an exception occurs, even if we have yet to reach max attempts. This is a useful place to do logging for example.

Unit tests and check code style

License

MIT

See Also


All versions of retry with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jbzoo/retry contains the following files

Loading the files please wait ....