eljam/circuit-breaker

A php Circuit Breaker

Maintainers

👁 eljam

Package info

github.com/eljam/circuit-breaker

pkg:composer/eljam/circuit-breaker

Statistics

Installs: 11 111

Dependents: 1

Suggesters: 0

Stars: 17

Open Issues: 0

v0.2.0 2017-12-22 18:06 UTC

Requires

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 653322e12e8b6ed9ee8a17d0406e0250580e78b5

  • eljam <guillaume.cavana.woop@gmail.com>

This package is auto-updated.

Last update: 2026-06-10 23:15:48 UTC


README

Circuit breaker is heavily used in microservice architecture to find issues between microservices calls.

The main idea is to protect your code from making unnecessary call if the microservice you call is down.

Features

  • Automatic update. (i.e you don't have to manually add success or failure method like other library)
  • Return result from the protected function
  • Retry timeout
  • Exclude some exceptions from being throwned, return null instead.
  • Multiprocess updates handled with a cache library. Supports all cache provider from (doctrine cache library).
  • Event powered

👁 Build Status
👁 Code Quality
👁 Code Coverage
👁 SensioLabsInsight
👁 Latest Unstable Version
👁 Latest Stable Version
👁 Downloads
👁 license

Full Example:

<?php

use Doctrine\Common\Cache\FilesystemCache;
use Eljam\CircuitBreaker\Breaker;
use Eljam\CircuitBreaker\Event\CircuitEvents;
use Symfony\Component\EventDispatcher\Event;

require_once __DIR__.'/vendor/autoload.php';

$fileCache = new FilesystemCache('./store', 'txt');

//Create a circuit for github api with a file cache and we want to exclude all exception.
$breaker = new Breaker('github_api', ['ignore_exceptions' => true], $fileCache);

$breaker->addListener(CircuitEvents::SUCCESS, function (Event $event) {
 $circuit = $event->getCircuit();
 echo "Success:".$circuit->getFailures()."\n";
});

$breaker->addListener(CircuitEvents::FAILURE, function (Event $event) {
 $circuit = $event->getCircuit();
 echo "Increment failure:".$circuit->getFailures()."\n";
});

$breaker->addListener(CircuitEvents::OPEN, function (Event $event) {
 $circuit = $event->getCircuit();
 echo sprintf("circuit %s is open \n", $circuit->getName());
});

$breaker->addListener(CircuitEvents::CLOSED, function (Event $event) {
 $circuit = $event->getCircuit();
 echo sprintf("circuit %s is closed \n", $circuit->getName());
});

$breaker->addListener(CircuitEvents::HALF_OPEN, function (Event $event) {
 $circuit = $event->getCircuit();
 echo sprintf("circuit %s is half-open \n", $circuit->getName());
});

$result = $breaker->protect(function () {
 throw new \Exception("An error as occured");
 // return 'ok';
});