Download the PHP package philiprehberger/http-retry-client without Composer
On this page you can find all versions of the php package philiprehberger/http-retry-client. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download philiprehberger/http-retry-client
More information about philiprehberger/http-retry-client
Files in philiprehberger/http-retry-client
Package http-retry-client
Short Description HTTP client wrapper with automatic retries, exponential backoff, and jitter
License MIT
Homepage https://github.com/philiprehberger/http-retry-client
Informations about the package http-retry-client
PHP HTTP Retry Client
HTTP client wrapper with automatic retries, exponential backoff, jitter, circuit breaker, and request logging.
Requirements
- PHP 8.2+
Installation
Usage
Basic Usage
Implement the HttpExecutor interface to wrap your preferred HTTP client:
Custom Retry Policy
Fluent Builder
Handling Retry Results
Jitter Modes
Control the jitter algorithm used for backoff delays:
Retry Hooks
Register callbacks that run before and after each retry attempt:
Custom Retryable Status Codes
Circuit Breaker
Protect downstream services with a circuit breaker that opens after consecutive failures:
The circuit breaker transitions through three states:
- Closed: Requests flow normally; failures are counted
- Open: Requests are rejected immediately with
CircuitBreakerOpenException - Half-Open: After the recovery timeout, one request is allowed through to test recovery
Request Logging
Log HTTP requests, responses, and failures for observability:
Timeout Configuration
Set connection and request timeouts:
API
RetryPolicy
| Parameter | Type | Default | Description |
|---|---|---|---|
maxRetries |
int |
3 |
Maximum number of retry attempts |
baseDelayMs |
int |
100 |
Base delay in milliseconds |
maxDelayMs |
int |
10000 |
Maximum delay cap in milliseconds |
multiplier |
float |
2.0 |
Backoff multiplier |
jitter |
bool |
true |
Whether to add random jitter |
retryableStatusCodes |
array<int> |
[429, 500, 502, 503, 504] |
Status codes that trigger a retry |
jitterMode |
JitterMode |
JitterMode::Full |
Jitter algorithm (Full, Equal, Decorrelated) |
beforeRetry |
?callable |
null |
Callback invoked before each retry (int $attempt, ?\Throwable $error) |
afterRetry |
?callable |
null |
Callback invoked after each retry (int $attempt, ?\Throwable $error) |
connectionTimeoutMs |
?int |
null |
Default connection timeout in milliseconds |
requestTimeoutMs |
?int |
null |
Default request timeout in milliseconds |
RetryPolicyBuilder
| Method | Description |
|---|---|
maxRetries(int $maxRetries) |
Set max retry attempts |
baseDelay(int $ms) |
Set base delay in milliseconds |
maxDelay(int $ms) |
Set max delay cap in milliseconds |
multiplier(float $multiplier) |
Set backoff multiplier |
withJitter(bool $jitter) |
Enable/disable jitter |
withoutJitter() |
Disable jitter |
retryOn(array $codes) |
Set retryable status codes |
jitterMode(JitterMode $mode) |
Set jitter algorithm |
beforeRetry(callable $callback) |
Register before-retry callback |
afterRetry(callable $callback) |
Register after-retry callback |
connectionTimeout(int $ms) |
Set connection timeout in milliseconds |
requestTimeout(int $ms) |
Set request timeout in milliseconds |
withCircuitBreaker(int $failureThreshold, int $recoveryTimeout) |
Enable circuit breaker |
withLogger(callable $logger, bool $logBodies) |
Enable request/response logging |
build() |
Build the RetryPolicy |
RetryClient
| Method | Description |
|---|---|
send(HttpRequest $request): RetryResult |
Send a request with automatic retries |
RetryResult
| Property | Type | Description |
|---|---|---|
statusCode |
int |
HTTP status code of the final response |
body |
string |
Response body |
headers |
array |
Response headers |
attempts |
int |
Total number of attempts made |
totalDelayMs |
int |
Total delay spent waiting (ms) |
wasRetried |
bool |
Whether the request was retried |
successful() |
bool |
Whether the status code is 2xx |
HttpExecutor (Interface)
| Method | Description |
|---|---|
execute(HttpRequest $request): HttpResponse |
Execute an HTTP request |
CircuitBreakerWrapper
| Method | Description |
|---|---|
__construct(int $failureThreshold, int $recoveryTimeoutSeconds) |
Create a circuit breaker |
execute(callable $action): mixed |
Execute action through the circuit breaker |
isOpen(): bool |
Check if circuit is open |
isClosed(): bool |
Check if circuit is closed |
state(): string |
Get current state (closed, open, half_open) |
reset(): void |
Reset to closed state |
failureCount(): int |
Get current failure count |
RequestLogger
| Method | Description |
|---|---|
__construct(callable $logger, bool $logBodies) |
Create a logger |
logRequest(HttpRequest $request, int $attempt): void |
Log an outgoing request |
logResponse(HttpResponse $response, int $attempt, float $durationMs): void |
Log a response |
logFailure(\Throwable $exception, int $attempt): void |
Log a failure |
MaxRetriesExceededException
| Property | Type | Description |
|---|---|---|
attempts |
int |
Total number of attempts made |
CircuitBreakerOpenException
Thrown when a request is rejected because the circuit breaker is in the open state.
Development
Support
If you find this project useful:
License
MIT