PHPackages                             haaragard/circuit-breaker - 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. haaragard/circuit-breaker

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

haaragard/circuit-breaker
=========================

A circuit breaker implementation package.

v0.3.0(12mo ago)421MITPHPPHP &gt;=8.0

Since Jul 3Pushed 12mo ago1 watchersCompare

[ Source](https://github.com/Haaragard/php-circuit-breaker)[ Packagist](https://packagist.org/packages/haaragard/circuit-breaker)[ RSS](/packages/haaragard-circuit-breaker/feed)WikiDiscussions main Synced today

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Circuit Breaker
===============

[](#circuit-breaker)

Installation
------------

[](#installation)

```
composer require haaragard/circuit-breaker
```

### Publish config file after installation:

[](#publish-config-file-after-installation)

*(optional)* Only needed for customizing the default configuration. (like using cache or creating new drivers).

```
php artisan vendor:publish --tag=haaragard-circuit-breaker-config
```

Supported laravel versions
--------------------------

[](#supported-laravel-versions)

- 10.x
- 11.x
- 12.x

How it works
------------

[](#how-it-works)

### Plug-and-play

[](#plug-and-play)

Using right after the installation is possible, as the package comes with a default configuration that uses the `LocalStorageAdapter` driver. This driver stores the circuit breaker state in-memory and is suitable for local development or testing purposes.

```
// Initialize the circuit breaker from the service container
$circuitBreaker = app()->get(CircuitBreakerInterface::class);

// Check if the circuit breaker is open
$isOpen = $circuitBreaker->isOpen('string-key');

// Record a failure
$circuitBreaker->recordFailure('string-key');

// Record a success
$circuitBreaker->recordSuccess('string-key');

// Reset
$circuitBreaker->reset('string-key');

// ForceReset
$circuitBreaker->forceReset('string-key');
```

You can also give any `string` as the first parameter to the methods above, which will be used as the circuit breaker name. This allows you to have multiple circuit breakers for different purposes.

### Changing the default config

[](#changing-the-default-config)

You can change the default configuration by publishing the config file and modifying it to your needs.

```
php artisan vendor:publish --tag=haaragard-circuit-breaker-config
```

### Configs

[](#configs)

Key: `enabled` - (`bool`) If set to `true`, the circuit breaker will be enabled. If set to `false`, it will be disabled.

```
