PHP code example of leyton / laravel-circuit-breaker

1. Go to this page and download the library: Download leyton/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/ */

    

leyton / laravel-circuit-breaker example snippets




return [
  'threshold' => 10, // number of trials to pass from half-open to closed/open and from closed to half-open
  'available_after_seconds' => 10, // the seconds it takes while in the open status
  'driver' => 'redis', // the cache store driver
];



use Leyton\LaravelCircuitBreaker\Circuit;
use Leyton\LaravelCircuitBreaker\Exceptions\RequestFailedException;


function goToGoogle(){
  try{
    $response = Http::get("https://gooogle.com");

    if($response->status() === 500){
        throw new RequestFailedException();
    }

  return "all is good";
  }catch(\Exception $exception){
    throw new RequestFailedException();
  }
}

// The Circuit is resolved out of the service container

$circuit = app()->make(Circuit::class);

//The run method expects the service name and the function that wraps the service
//It should throw the RequestFailedException when the service is not responding as expected
$packet =  $circuit->run("go-to-google", fn() => goToGoogle());


Leyton\LaravelCircuitBreaker\Transporters\Packet {#2939
    +result: "all is good",
    +status: Leyton\LaravelCircuitBreaker\CircuitStatus {#2943
        +name: "CLOSED",
        +value: "closed",
    },
    +success: true,
}




namespace App\Http\Controllers;

use Leyton\LaravelCircuitBreaker\Circuit;
use App\Services\LocationServiceClient;
use App\Services\PaymentGatewayClient;
use Illuminate\Http\Request;
use Exception;


class MakeOrderController extends Controller
{
    public function __construct(
        protected LocationServiceClient $locationServiceClient,
        protected PaymentGatewayClient $paymentGatewayClient,
        protected Circuit $Circuit,
    ) {
        
    }

    public function __invoke(Request $request)
    {
        $location = $request->get('location');
        $paymentDetails = $request->get('payment_details');
        $client = $request->get('client');

        if(!$this->circuit->available(['location-service', 'payment-service'])){
            return response()->json([
                'message' => 'Services are un-available, please retry later'
            ]);
        }
        
        $withdrawalPoint = $this->circuit->run("location-service", fn() => $this->locationServiceClient->getNearWithdrawalPoint($location));
        
        $payment = $this->circuit->run(
                "payment-service", 
                fn() => $this->paymentGatewayClient->processPayment($client, $order, $withdrawalPoint->result)
        );

    // ...
        return response()->json($data);
    }
}

`
php artisan vendor:publish --provider="Leyton\LaravelCircuitBreaker\LaravelCircuitBreakerServiceProvider"
`config/circuit-breaker.php