PHPackages                             leocarmo/circuit-breaker-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. leocarmo/circuit-breaker-php

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

leocarmo/circuit-breaker-php
============================

Circuit Breaker for PHP

6.0.1(10mo ago)300447.3k—3%28[1 issues](https://github.com/leocarmo/circuit-breaker-php/issues)[2 PRs](https://github.com/leocarmo/circuit-breaker-php/pulls)3MITPHPPHP ^8.0CI passing

Since Jun 30Pushed 10mo ago2 watchersCompare

[ Source](https://github.com/leocarmo/circuit-breaker-php)[ Packagist](https://packagist.org/packages/leocarmo/circuit-breaker-php)[ RSS](/packages/leocarmo-circuit-breaker-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (6)Versions (14)Used By (3)

PHP implementation of Circuit Breaker Pattern
=============================================

[](#php-implementation-of-circuit-breaker-pattern)

[![Build Status](https://camo.githubusercontent.com/e089ae8f30fa6883066a69a5a3d255041ecae54186c6b193baa435feb65914df/68747470733a2f2f7472617669732d63692e6f72672f6c656f6361726d6f2f636972637569742d627265616b65722d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/leocarmo/circuit-breaker-php)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/e123f640be621e15fdf4422fde830bd57ed24d805f89ec286b4ff95bff16b85f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c656f6361726d6f2f636972637569742d627265616b65722d7068702f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/leocarmo/circuit-breaker-php/?branch=master)[![Code Intelligence Status](https://camo.githubusercontent.com/3836bd348e163bc60cb11305054139aef0160fc2077a8130267df6c942920ee8/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c656f6361726d6f2f636972637569742d627265616b65722d7068702f6261646765732f636f64652d696e74656c6c6967656e63652e7376673f623d6d6173746572)](https://scrutinizer-ci.com/code-intelligence)[![Total Downloads](https://camo.githubusercontent.com/a3e6427005a8bc8ed733771ebb3b817fa41a8efa6f211b0a2e94f0e16270966d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c656f6361726d6f2f636972637569742d627265616b65722d7068702e737667)](https://packagist.org/packages/leocarmo/circuit-breaker-php)

For more information about this pattern see [this](https://martinfowler.com/bliki/CircuitBreaker.html).

Starting with composer
----------------------

[](#starting-with-composer)

```
composer require leocarmo/circuit-breaker-php
```

Adapters
--------

[](#adapters)

- [Redis](#redis-adapter)
- [Redis Cluster](#redis-cluster-adapter)
- [Swoole Table](#swooletable-adapter)

### Redis Adapter

[](#redis-adapter)

The first argument is a redis connection, the second is your product name, for redis namespace avoid key conflicts with another product using the same redis.

```
use LeoCarmo\CircuitBreaker\CircuitBreaker;
use LeoCarmo\CircuitBreaker\Adapters\RedisAdapter;

// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);

$adapter = new RedisAdapter($redis, 'my-product');

// Set redis adapter for CB
$circuit = new CircuitBreaker($adapter, 'my-service');
```

> See [this](examples/RedisAdapterExample.php) for full example

### Redis Cluster Adapter

[](#redis-cluster-adapter)

Without use of [`multi`](https://redis.io/commands/multi/) command. The first argument is a redis connection, the second is your product name, for redis namespace avoid key conflicts with another product using the same redis.

```
use LeoCarmo\CircuitBreaker\CircuitBreaker;
use LeoCarmo\CircuitBreaker\Adapters\RedisClusterAdapter;

// Connect to redis
$redis = new \Redis();
$redis->connect('localhost', 6379);

$adapter = new RedisClusterAdapter($redis, 'my-product');

// Set redis adapter for CB
$circuit = new CircuitBreaker($adapter, 'my-service');
```

> See [this](examples/RedisClusterAdapterExample.php) for full example

### SwooleTable Adapter

[](#swooletable-adapter)

```
use LeoCarmo\CircuitBreaker\CircuitBreaker;

$circuit = new CircuitBreaker(new SwooleTableAdapter(), 'my-service');
```

Guzzle Middleware
-----------------

[](#guzzle-middleware)

```
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use LeoCarmo\CircuitBreaker\GuzzleMiddleware;

$handler = new GuzzleMiddleware($circuit);

$handlers = HandlerStack::create();
$handlers->push($handler);

$client = new Client(['handler' => $handlers]);

$response = $client->get('leocarmo.dev');
```

*Important*: all status code between 200 and 299 will be recorded as a success, and other status will be recorded as a failure.

> See [this](examples/GuzzleMiddlewareExample.php) for full example

### Customize success status code

[](#customize-success-status-code)

If you need to specify a custom status code that is not a failure, you can use:

```
$handler = new GuzzleMiddleware($circuit);
$handler->setCustomSuccessCodes([400]);
```

*Important:* this configuration will record a success when a status code `400` is returned

> See [this](examples/GuzzleMiddlewareCustomCodeExample.php) for full example

### Ignore status code

[](#ignore-status-code)

If you want to ignore the status code returned and not record a success or failure, use this:

```
$handler = new GuzzleMiddleware($circuit);
$handler->setCustomIgnoreCodes([412]);
```

Important

To use `Customize success status code` or `Ignore status code` you must set the [Guzzle client config `http_errors = false`](https://docs.guzzlephp.org/en/stable/request-options.html#http-errors) because the Guzzle Client throws a `ClientException` or `ServerException` when the status code is not in range `>200 &&  This is not required, default values will be set

```
$circuit->setSettings([
    'timeWindow' => 60, // Time for an open circuit (seconds)
    'failureRateThreshold' => 50, // Fail rate for open the circuit
    'intervalToHalfOpen' => 30,  // Half open time (seconds)
]);
```

Check if circuit is available (closed)
--------------------------------------

[](#check-if-circuit-is-available-closed)

Each check is for a specific service. So you can have multiple services in the same application, and when one circuit is open, the other works normally.

```
// Check circuit status for service
if (! $circuit->isAvailable()) {
    die('Circuit is not available!');
}
```

Record success and failure
--------------------------

[](#record-success-and-failure)

```
// Usage example for success and failure
try {
    myService();
    $circuit->success();
} catch (RuntimeException $e) {
    // If an error occurred, it must be recorded as failure.
    $circuit->failure();
}
```

Development
-----------

[](#development)

### Setup

[](#setup)

```
make setup
```

### Tests

[](#tests)

```
make test
```

Contributors
------------

[](#contributors)

[ ![](https://camo.githubusercontent.com/a31c17943f20d30f7286ae403eb95178bf1d9d61c2a691c82ce6b005b7465c37/68747470733a2f2f636f6e747269622e726f636b732f696d6167653f7265706f3d6c656f6361726d6f2f636972637569742d627265616b65722d706870266d61783d3130)](https://github.com/leocarmo/circuit-breaker-php/graphs/contributors)

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance54

Moderate activity, may be stable

Popularity55

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 81.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~199 days

Recently: every ~329 days

Total

12

Last Release

321d ago

Major Versions

1.0.0 → 2.0.02019-06-30

2.0.3 → 3.0.02020-12-21

3.0.0 → 4.0.02021-11-18

4.0.2 → 5.0.02022-12-14

5.0.0 → 6.0.02025-04-14

PHP version history (4 changes)1.0.0PHP &gt;=7

2.0.0PHP &gt;=7.1

3.0.0PHP ^7.4 || ^8.0

5.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/18bacea56d6c1c0be251f3293138255ff9212d19eb665fda3712732d23e688c6?d=identicon)[leocarmo](/maintainers/leocarmo)

---

Top Contributors

[![leocarmo](https://avatars.githubusercontent.com/u/21243754?v=4)](https://github.com/leocarmo "leocarmo (79 commits)")[![celosauro](https://avatars.githubusercontent.com/u/52496242?v=4)](https://github.com/celosauro "celosauro (10 commits)")[![afraprg](https://avatars.githubusercontent.com/u/3180952?v=4)](https://github.com/afraprg "afraprg (4 commits)")[![kodmaster23](https://avatars.githubusercontent.com/u/14351322?v=4)](https://github.com/kodmaster23 "kodmaster23 (3 commits)")[![wandersonwhcr](https://avatars.githubusercontent.com/u/5286703?v=4)](https://github.com/wandersonwhcr "wandersonwhcr (1 commits)")

---

Tags

circuit-breakercircuit-breaker-patternfailure-handling

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/leocarmo-circuit-breaker-php/health.svg)

```
[![Health](https://phpackages.com/badges/leocarmo-circuit-breaker-php/health.svg)](https://phpackages.com/packages/leocarmo-circuit-breaker-php)
```

###  Alternatives

[google/cloud-core

Google Cloud PHP shared dependency, providing functionality useful to all components.

343121.4M79](/packages/google-cloud-core)[aimeos/aimeos-base

Aimeos base layer for abstracting from host environments

2.1k134.0k1](/packages/aimeos-aimeos-base)[phpgt/dom

Modern DOM API.

12412.2M18](/packages/phpgt-dom)[anthropic-ai/sdk

Anthropic PHP SDK

129134.7k5](/packages/anthropic-ai-sdk)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73142.3k25](/packages/jaxon-php-jaxon-core)[aedart/athenaeum

Athenaeum is a mono repository; a collection of various PHP packages

245.2k](/packages/aedart-athenaeum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
