Download the PHP package gregpriday/laravel-retry without Composer

On this page you can find all versions of the php package gregpriday/laravel-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 laravel-retry

Laravel Retry

Latest Version on Packagist Total Downloads Code Style

A powerful, flexible, and deeply integrated retry system for Laravel applications that goes beyond simple retry loops. This package provides sophisticated retry strategies, deep Laravel integration, and comprehensive observability to make your applications more resilient to transient failures.

Introduction

In modern web applications, dealing with external services, APIs, and databases is commonplace. However, these interactions can fail due to temporary issues like network glitches, rate limits, or service unavailability. Laravel Retry provides a robust solution to handle these transient failures elegantly and efficiently.

What sets Laravel Retry apart:

Key Features

Real-World Problem Solving

Laravel Retry excels at solving common but tricky scenarios:

Whether you're building a robust API client, managing complex workflows, or just want to make your application more resilient, Laravel Retry provides the tools and flexibility you need.


Table of Contents


Installation

Requirements

Quick Start

  1. Install the package via Composer:

  2. The package uses Laravel's auto-discovery, so the service provider and facade will be automatically registered. If you have disabled auto-discovery, manually add the following to your config/app.php:

Configuration

  1. Publish the configuration file:

This will create a config/retry.php file in your application's configuration directory, where you can customize retry settings.

Configuration Summary

After publishing, you can customize the package's behavior in config/retry.php. Here are the key options:

Available built-in strategy aliases:

For full configuration details, refer to the published config/retry.php file.

  1. (Optional) Publish exception handlers:

This will copy the built-in exception handlers to your application's app/Exceptions/Retry/Handlers directory, allowing you to customize them or use them as templates for your own handlers.

Get Started in 2 Minutes

Here's a minimal example to get started with Laravel Retry:

That's it! Laravel Retry will automatically handle common HTTP exceptions and retry with exponential backoff.


Basic Usage

Simple Retry Operation

The most basic way to use Laravel Retry is through the Retry facade:

You can customize retry behavior using a fluent interface:

The RetryResult Object

The Retry::run() method returns a RetryResult object that provides a promise-like interface for handling the operation's outcome. This avoids nested try/catch blocks and makes your code more readable.

Available methods on the RetryResult object:

Method Description
then(Closure $callback) Executes the callback if the operation succeeds, passing the operation's result as its parameter. Returns a new RetryResult with the callback's return value.
catch(Closure $callback) Executes the callback if the operation fails, passing the exception as its parameter. Returns a new RetryResult with the callback's return value.
finally(Closure $callback) Executes the callback regardless of whether the operation succeeds or fails. The callback receives no parameters. Returns the original RetryResult.
value() Returns the operation's result directly. If the operation failed, throws the last exception that was caught.
throw() Same as value() but with a more explicit name when you expect an exception may be thrown.
throwFirst() Returns the result directly, but if the operation failed, throws the first exception that was caught instead of the last one.

Example with the finally method:

Example with comprehensive error handling:

Configuring Retry Operations

Laravel Retry provides several configuration options through fluent methods that allow you to customize how retry operations behave. These methods override the global configuration defined in config/retry.php for a specific operation:

Available Configuration Methods

Fluent Method Description Config Key
maxRetries(int $retries) Sets maximum number of retry attempts after the initial try. max_retries
timeout(int $seconds) Sets maximum execution time per attempt in seconds. timeout
withStrategy(RetryStrategy $strategy) Specifies a custom retry strategy to use. N/A
retryIf(Closure $condition) Custom callback to determine if a retry should occur. N/A
retryUnless(Closure $condition) Custom callback to determine when a retry should NOT occur. N/A
withProgress(Closure $callback) Register callback for progress reporting during retries. N/A
withEventCallbacks(array $callbacks) Register callbacks for retry lifecycle events. N/A
withMetadata(array $metadata) Add custom data to the retry context. N/A

Understanding Delay Configuration

The delay between retry attempts is primarily controlled by the retry strategy used. The most important parameter across many strategies is baseDelay, which serves as the foundation for delay calculations:

How baseDelay is interpreted depends on the strategy:

You can configure baseDelay (and other strategy options) in several ways, listed by precedence (later options override earlier ones):

  1. Globally per Strategy Alias: Define the default constructor parameters, including baseDelay, for each strategy alias in config/retry.php under the strategies key (e.g., config('retry.strategies.exponential-backoff.baseDelay')).
  2. When Instantiating a Strategy Directly: Pass baseDelay as a named argument to the strategy's constructor (e.g., new ExponentialBackoffStrategy(baseDelay: 0.5)).
  3. In HTTP Client Macros: Use the base_delay option within the options array passed to macros like robustRetry or withRetryStrategy (e.g., Http::robustRetry(3, null, ['base_delay' => 0.75])).
  4. In Pipeline Stages: Provide a custom RetryStrategy instance within a pipeline stage class, configured with its specific baseDelay in its constructor.

Example with different strategies (direct instantiation):

Most strategies in the library also support additional configuration parameters like maxDelay, multiplier, increment, withJitter, etc., that can be configured similarly (in config/retry.php under the specific strategy alias or via constructor parameters) to further customize the delay behavior. Refer to the config/retry.php file and the individual strategy classes for available options.

HTTP Client Integration

Laravel Retry extends Laravel's HTTP Client with custom macros like robustRetry, withCircuitBreaker, and withRateLimitHandling. These macros are automatically registered when the package is installed, allowing you to use sophisticated retry patterns directly in your HTTP requests:

Note: The max_attempts parameter in HTTP client macros specifies the total number of attempts (initial + retries), whereas Retry::maxRetries() specifies the number of additional retries after the first attempt.

Additional HTTP Client Macros

Laravel Retry provides other specialized macros for common patterns:

Circuit Breaker

Use the Circuit Breaker pattern to prevent overwhelming failing services:

Rate Limiting

Control the rate of requests to avoid overwhelming services:

These macros can be combined with other Laravel HTTP client features:

Pipeline Integration

For complex workflows where multiple steps need retry capabilities, use the RetryablePipeline:

Each stage in the pipeline can have its own retry configuration by defining public properties:


Advanced Configuration

Retry Strategies

Laravel Retry comes with a comprehensive set of retry strategies to handle different scenarios. Each strategy implements the RetryStrategy interface and can be used with the Retry facade, HTTP client, or pipeline integration.

Strategy Overview

Strategy Alias Primary Use Case
ExponentialBackoffStrategy exponential-backoff Handles general temporary failures by exponentially increasing the delay between retries.
LinearBackoffStrategy linear-backoff Provides a predictable retry delay that increases by a fixed amount with each attempt.
FibonacciBackoffStrategy fibonacci-backoff Offers a balanced retry delay growth based on the Fibonacci sequence, suitable for various scenarios.
FixedDelayStrategy fixed-delay Applies a consistent, fixed delay between every retry attempt, ideal for predictable recovery times.
DecorrelatedJitterStrategy decorrelated-jitter Prevents retry collisions ("thundering herd") in high-traffic scenarios using AWS-style decorrelated jitter.
GuzzleResponseStrategy guzzle-response Intelligently retries HTTP requests based on standard response headers like Retry-After or X-RateLimit-Reset.
ResponseContentStrategy response-content Triggers retries by inspecting response content (like JSON error codes or text patterns) even when the HTTP status is successful.
CircuitBreakerStrategy circuit-breaker Prevents overwhelming a failing service by temporarily halting requests after repeated failures (Circuit Breaker pattern).
RateLimitStrategy rate-limit Controls retry frequency to respect API rate limits or manage load on internal services using Laravel's Rate Limiter.
TotalTimeoutStrategy total-timeout Ensures the entire retry operation (including delays) completes within a specific total time limit.
CustomOptionsStrategy custom-options Allows customizing an existing strategy's behavior with specific options and callbacks for one-off adjustments without extending base classes. Perfect for specific, fine-grained control over retry logic using closures.
CallbackRetryStrategy callback-retry Enables completely custom retry logic by defining both the delay calculation and the retry decision logic via callbacks.

Available strategies:

1. ExponentialBackoffStrategy (Alias: exponential-backoff)

Increases delay exponentially with each attempt. Best for general-purpose retries and temporary networking or service issues where increasing wait times helps recovery.

2. LinearBackoffStrategy (Alias: linear-backoff)

Increases delay by a fixed amount with each attempt. Useful when a more predictable increase in wait time is desired compared to exponential backoff.

3. FibonacciBackoffStrategy (Alias: fibonacci-backoff)

Increases delay according to the Fibonacci sequence. Good balance between aggressive and conservative retries, growing slower than exponential but faster than linear initially.

4. FixedDelayStrategy (Alias: fixed-delay)

Uses the same delay for every retry attempt. Ideal when the expected recovery time is consistent or when predictable delays are needed.

5. DecorrelatedJitterStrategy (Alias: decorrelated-jitter)

Implements AWS-style jitter for better distribution of retries. Excellent for high-traffic scenarios to prevent the "thundering herd" problem where many clients retry simultaneously.

6. GuzzleResponseStrategy (Alias: guzzle-response)

Intelligent HTTP retry strategy that respects response headers. Perfect for APIs that provide retry guidance through headers like Retry-After or X-RateLimit-Reset.

7. ResponseContentStrategy (Alias: response-content)

Inspects response bodies for error conditions, even on successful status codes. Use for APIs that return success HTTP codes (200 OK) but signal errors via JSON response body.

8. CircuitBreakerStrategy (Alias: circuit-breaker)

Implements the Circuit Breaker pattern to prevent overwhelming failing services. After a threshold of failures, it "opens" and temporarily stops attempts, allowing the service to recover.

9. RateLimitStrategy (Alias: rate-limit)

Uses Laravel's Rate Limiter to control retry attempts. Ideal for respecting API rate limits or managing load on internal services.

10. TotalTimeoutStrategy (Alias: total-timeout)

Enforces a maximum total duration for the entire retry operation. Use when an operation must complete within a strict time budget, regardless of individual attempt results.

11. CustomOptionsStrategy (Alias: custom-options)

Allows customizing an existing strategy's behavior with specific options and callbacks for one-off adjustments without extending base classes. Perfect for specific, fine-grained control over retry logic using closures.

12. CallbackRetryStrategy (Alias: callback-retry)

A fully callback-driven strategy where you define both the delay calculation and the retry decision logic via closures. Ideal for completely custom retry patterns without needing a base strategy.

Combining Strategies

Many strategies can be combined by wrapping one strategy inside another:

Note on Wrapper Strategies: When using wrapper strategies (like CircuitBreaker, RateLimit, TotalTimeout) via aliases or HTTP macros, configuration options like baseDelay typically apply to the default inner strategy. When instantiating wrappers directly as shown above, configure the inner strategy explicitly with its own parameters.

Exception Handling

Laravel Retry provides a sophisticated exception handling system that determines whether a failure should trigger a retry attempt. The system is extensible and configurable to match your specific needs.

Exception Handler Discovery

The package uses an automatic discovery mechanism to find and register exception handlers from:

  1. Built-in handlers in the package
  2. Custom handlers in your application's app/Exceptions/Retry/Handlers directory
  3. Any additional directories specified in the handler_paths array in config/retry.php

Each handler determines which specific exceptions or error patterns should trigger a retry attempt.

Built-in Exception Handling

By default, the package includes handlers for common scenarios:

The built-in handlers cover common cases like:

Custom Retry Conditions

You can define custom conditions for retrying:

Example combining a custom handler with retryIf:

Creating Custom Exception Handlers

For more reusable exception handling, implement the RetryableExceptionHandler interface:

  1. Create a new handler in app/Exceptions/Retry/Handlers:

  2. The handler will be automatically discovered and registered by the ExceptionHandlerManager.

Adding Custom Handler Paths

If you want to organize your handlers in different locations, add the paths to the handler_paths array in config/retry.php:

Disabling Specific Handlers

You can temporarily disable specific handler types by extending the base handler and overriding the isApplicable() method:

Events & Monitoring

Laravel Retry dispatches events at key points in the retry lifecycle, allowing you to monitor and respond to retry operations.

Available Events

Laravel Retry dispatches the following events:

  1. RetryingOperationEvent: Dispatched before each retry attempt, contains information about the current attempt, the delay before the retry, and the exception that caused the retry.
  2. OperationSucceededEvent: Dispatched when the operation succeeds, contains the final result, the number of attempts, and performance metrics.
  3. OperationFailedEvent: Dispatched when all retries are exhausted and the operation ultimately fails, contains the final exception, exception history, and performance metrics.

Each event includes a RetryContext object containing detailed information about the retry operation.

The RetryContext Object

The RetryContext object provides comprehensive information about the retry operation:

Property/Method Description
getOperationId() Returns the unique identifier for this specific retry operation
getMetadata() Returns any custom data added via withMetadata()
getMetrics() Returns performance metrics including total_duration, average_duration, total_delay, min_duration, max_duration, and total_elapsed_time for monitoring and analytics. Details:

Practical Event Use Cases

Events provide powerful hooks into the retry lifecycle, enabling various monitoring and operational tasks:

Using Events

Register listeners in your EventServiceProvider:

Or use inline event callbacks:

Example Event Listener

Here's an example of a listener for the OperationFailedEvent:


Troubleshooting

Here are solutions to common issues you might encounter:

Operation Isn't Retrying When Expected

Custom Exception Handler Not Being Used

HTTP Client Macros Not Working

Pipeline Stage Retries Using Incorrect Settings

Events Not Firing

If problems persist, check your Laravel logs (storage/logs/laravel.log) and consider enabling the withProgress() callback for more verbose output during retries.


Contributing

We welcome contributions to Laravel Retry! Here's how you can help:

Reporting Issues

If you discover a bug or have a feature request:

  1. Search the GitHub issues to see if it has already been reported.
  2. If not, create a new issue with as much detail as possible.

Pull Requests

  1. Fork the repository
  2. Create a new branch for your feature or bug fix
  3. Write tests for your changes
  4. Ensure all tests pass by running vendor/bin/phpunit
  5. Ensure code style compliance by running vendor/bin/pint
  6. Submit a pull request with a clear description of your changes

Development Setup

Code Style

This package follows the Laravel coding style. We use Laravel Pint for code formatting:


License

Laravel Retry is open-sourced software licensed under the MIT license.


All versions of laravel-retry with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^10.0|^11.0|^12.0
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 gregpriday/laravel-retry contains the following files

Loading the files please wait ....