API Network CircuitBreakerInterface - evansims/openfga-php GitHub Wiki
Circuit breaker interface for preventing cascade failures in distributed systems. This interface defines the contract for circuit breaker implementations that temporarily disable requests to failing endpoints, preventing resource exhaustion and allowing time for recovery. Circuit breakers track failures per endpoint and automatically open/close based on failure thresholds and cooldown periods. The circuit breaker operates in three states: - Closed: Normal operation, requests are allowed - Open: Failures exceeded threshold, requests are blocked - Half-Open: After cooldown, limited requests allowed to test recovery
Table of Contents
OpenFGA\Network
- CircuitBreaker (implementation)
public function getFailureCount(string $endpoint): int
Get the current failure count for an endpoint. Returns the number of consecutive failures recorded for the specified endpoint. This can be useful for logging and monitoring purposes.
Name | Type | Description |
---|---|---|
$endpoint |
string |
The endpoint URL or identifier to check |
int
— The current failure count (0 if no failures recorded)
public function isOpen(string $endpoint): bool
Check if the circuit is currently open for an endpoint. Returns true if the circuit breaker is currently blocking requests to the specified endpoint due to excessive failures.
Name | Type | Description |
---|---|---|
$endpoint |
string |
The endpoint URL or identifier to check |
bool
— True if the circuit is open (blocking requests), false otherwise
public function recordFailure(string $endpoint): void
Record a failure for the specified endpoint. Increments the failure count for the endpoint and updates the failure timestamp. If the failure threshold is reached, the circuit will open and block subsequent requests until the cooldown period expires.
Name | Type | Description |
---|---|---|
$endpoint |
string |
The endpoint URL or identifier that failed |
void
public function recordSuccess(string $endpoint): void
Record a successful request for the specified endpoint. Resets the failure state for the endpoint, effectively closing the circuit and allowing normal operation to resume. This should be called whenever a request succeeds after previous failures.
Name | Type | Description |
---|---|---|
$endpoint |
string |
The endpoint URL or identifier that succeeded |
void
public function shouldRetry(string $endpoint): bool
Check if the circuit breaker should allow a request to the specified endpoint. Evaluates whether a request should be allowed based on the current circuit state for the given endpoint. If the cooldown period has passed, the circuit is automatically reset to allow new attempts.
Name | Type | Description |
---|---|---|
$endpoint |
string |
The endpoint URL or identifier to check |
bool
— True if requests should be allowed, false if the circuit is open