Download the PHP package endisha/resilient-client without Composer
On this page you can find all versions of the php package endisha/resilient-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package resilient-client
Resilient Client
Resilient Client is a PHP package designed to implement a circuit breaker pattern to enhance stability and prevent cascading failure requests.
What is circuit breaker pattern?
The Circuit Breaker is a design pattern used in software development. It is used to detect and prevent cascading request failures due to slow network connectivity, timeouts, unavailability, or unexpected system difficulties.
Requirements
- PHP ^8.1 or higher
Installation
You can install the package via Composer by running the following command:
Usage
To use the package in your project, include the Composer autoload file:
Implementing the circuit breaker pattern:
The CircuitBreaker
constructor also allows setting a custom key as the second parameter; the default value is default
Adapters
The package supports two types of adapters, each serving as a method of state storage for the circuit breaker pattern:
- File Storage Adapter: Stores states within files.
- PDO Database Adapter: Uses PDO to store states in a database.
File Storage Adapter
To use the File Storage Adapter and save circuit states to files use FileStorageAdapter
and follow these steps:
Database (PDO) Adapter
To use the Database (PDO) Adapter and work with PDO to save states in the circuit_breaker
table, you need to create the custom table first:
Create an instance of PDODatabaseAdapter
class
How it Works
-
To check if the circuit is open (not available), use:
-
To mark a request as successful:
- To mark a request as failed:
Example
Guzzle Integration
The package supports integration with Guzzle through the CircuitBreakerGuzzleMiddleware
middleware. This middleware is automatically implemented when ConnectException
exceptions are thrown due to problems connecting to remote servers or establishing connections. To implement it, follow these steps:
Additionally, you can specify certain HTTP status codes as failure requests. For example, you might want to treat errors like 301 (Moved Permanently) and 302 (Found) as failure responses:
Handling Exceptions
When the circuit breaker is in an open state, it throws the ResilientClient\CircuitBreaker\Exceptions\CircuitBreakerException
exception class. This exception serves as a signal that the circuit is currently open and requests to the service are not allowed. You can catch this exception to implement custom handling logic in your application when the circuit is open.
Example usage:
Tests
License
The Resilient Client package is open-source software licensed under the MIT License.