You are viewing documentation for an outdated version. It is no longer supported!

Circuits

TIP

Circuits package is available from Athenaeum v4.2

This package offers a Circuit Breaker that can be used to "[...] detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties[...]" (wikiopen in new window).

A detailed explanation of how the Circuit Breaker Pattern works, can be found on Mediumopen in new window and Martin Fowler's blogopen in new window

Example

<?php

use Aedart\Circuits\Traits\CircuitBreakerManagerTrait;

class WeatherService
{
    use CircuitBreakerManagerTrait;

    public function forecast()
    {
        $circuitBreaker = $this->getCircuitBreakerManager()
                ->create('weather_service');

        return $circuitBreaker->attempt(function() {
            // Perform 3rd party API call... not shown here
        }, function(){
            // Service has failed and is unavailable, do something else...
        });
    }
}