PHP code example of omidrezasalari / circuit-breaker-bundle

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

    

omidrezasalari / circuit-breaker-bundle example snippets


return [
    // Other bundles...
    Omidrezasalari\CircuitBreakerBundle\CircuitBreakerBundle::class => ['all' => true],
];

namespace App\Service;

use Omidrezasalari\CircuitBreakerBundle\Service\StorageInterface;

class CustomStorage implements StorageInterface
{
    // Implement 

use Omidrezasalari\CircuitBreakerBundle\Service\CircuitBreaker;

class YourService
{
    private CircuitBreaker $circuitBreaker;

    public function __construct(CircuitBreaker $circuitBreaker)
    {
        $this->circuitBreaker = $circuitBreaker;
    }

    public function someOperation()
    {
        if ($this->circuitBreaker->isOpen('your_service')) {
            throw new \Exception("Circuit breaker is open. Operation not allowed.");
        }

        try {
            // Perform the operation
        } catch (\Exception $e) {
            $this->circuitBreaker->attemptFailure('your_service');
            throw $e;
        }

        // If operation succeeds
        $this->circuitBreaker->attemptSuccess('your_service');
    }
}
`
sudo apt install php-apcu
`
brew install php-apcu