PHP code example of alex97lewis / laravel-circuit-breaker

1. Go to this page and download the library: Download alex97lewis/laravel-circuit-breaker library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alex97lewis / laravel-circuit-breaker example snippets


use Alex97Lewis\CircuitBreaker\CircuitBreaker;

$circuitBreaker = new CircuitBreaker('api-service');

try {
    $result = $circuitBreaker->call(function() {
        // Your risky operation here
        return Http::get('https://external-api.com/data');
    });
} catch (CircuitBreakerOpenException $e) {
    // Circuit is open, handle gracefully
    return $fallbackData;
}

// Simple helper
$result = with_circuit_breaker(function() {
    return Http::get('https://external-api.com/data');
}, 'api-service');

// Get circuit breaker instance
$cb = circuit_breaker('database-service', [
    'failure_threshold' => 3,
    'recovery_timeout' => 30
]);

use Alex97Lewis\CircuitBreaker\CircuitBreakerFacade as CircuitBreaker;

$result = CircuitBreaker::call(function() {
    return SomeService::riskyOperation();
});

// Check state
if (CircuitBreaker::isAvailable()) {
    // Safe to proceed
}
bash
php artisan vendor:publish --tag=circuit-breaker-config