Pool::setCircuitBreakerStrategy

(PHP 8.6+, True Async 1.0)

public Pool::setCircuitBreakerStrategy(?CircuitBreakerStrategy $strategy): void

Sets the Circuit Breaker strategy for the pool. The Circuit Breaker monitors the availability of an external service: upon detecting multiple failures, the pool automatically transitions to the INACTIVE state, preventing a cascade of errors. When the service recovers, the pool returns to an active state.

Parameters

strategy
A CircuitBreakerStrategy object defining the rules for transitioning between states. null — disable Circuit Breaker.

Return Value

No value is returned.

Examples

Example #1 Setting a strategy

<?php

use Async\Pool;
use Async\CircuitBreakerStrategy;

$pool = new Pool(
    factory: fn() => new HttpClient('https://api.example.com'),
    destructor: fn(HttpClient $c) => $c->close(),
    max: 10
);

$strategy = new CircuitBreakerStrategy(
    failureThreshold: 5,       // after 5 errors — deactivate
    recoveryTimeout: 30000,    // after 30 seconds — attempt recovery
    successThreshold: 3        // 3 successful requests — full activation
);

$pool->setCircuitBreakerStrategy($strategy);

Example #2 Disabling Circuit Breaker

<?php

use Async\Pool;

// Disable the strategy
$pool->setCircuitBreakerStrategy(null);

See Also