Code Monkey home page Code Monkey logo

circuitbreaker's Introduction

PHP CircuitBreaker (v 0.1)

This implementation of the CircuitBreaker pattern provide a mechanism for resilience against a cascade of failures which could adversely affect an application's performance. The CircuitBreaker monitors successful and non-successful transactions then provides feedback, by way of tripping (opening) the circuit breaker, if the failures reach a certain threshold.

To construct a circuit breaker (with ArrayPersistence storage) use:

$breaker = new Breaker('breakerName', new ArrayPersistence);

...or with parameters:

Breaker::build(
    'testBreaker',
    new ArrayPersistence,
    array(
        'timeout' => 300,
        'threshold' => 1,
        'will_retry' => false
    )
);

...or set the values long-hand:

$breaker->setThreshold(1);
$breaker->setTimeout(300);
$breaker->setWillRetryAfterTimeout(false);

The default values are:

  • Breaker closed on create: true
  • Threshold: 5 failures before the circuit breaker opens
  • Timeout: 60s (1 minute) before calls to isClosed starts to return a 'half-open' response
  • Will Retry: true/the circuit will attempt re-tries after the timeout has expired

The circuit breaker is used with the code:

if ($breaker->isClosed()) {
    try {
        // YOUR EXPENSIVE CALL HERE
        $breaker->success();

    } catch (Exception $e) {
        // log a failure
        $breaker->failure();
    }
} else {
    // FALLBACK TO SOMETHING ELSE HERE
}

If the system reaches a threshold of registered failures (the client may be experiencing something such as a back-end not responding) it will 'trip' open the circuit so subsequent calls to isClosed() return false (or it's opposite isOpen() method returns true). This can prevent the situation where requests time out which can cause more issues by continually attempting requests on services which are unavailable. So any subsequent requests can fail quickly and by handled by the client.

If the circuit breaker opens due to the threshold being reached or by an explicit call to open(), the isClosed() method will return false. However if you subsequently register a successful transaction then it will re-close the circuit breaker, assuming the problems have now resolved and your code is able to execute. This is so systems are able to recover if the upstream problem is resolved.

If 'will retry' [set/getWillRetryAfterTimeout()] is set to true, any calls to isClosed() will return a true if the timeout [set/getTimeout()] has expired since the last registered failure. This is so systems are given the chance to recover and the circuit breaker can be re-closed if upstream services begin working again. In this 'half-open' mode, the client can try another request; if it fails then the timeout will be reset. If successful, the client can register that success by calling success() which will close the breaker properly.

It 'will retry' is set to false the breaker will remain open until explicitly closed with a successful transaction registered by calling successs() or by calling the explicit close() method (see 'Short-cut Operation').

Short-cut Operation

The circuit breaker can be immediately tripped by using:

$breaker->open();

...and closed:

$breaker->close();

...or reset:

$breaker->reset();

...which re-closes the circuit breaker and zeros the failure counter. Note that this does not reset the last failure timestamp which is only ever set by an actual failure.

Persistence

Transaction counts are stored using implementation defined by the PersistenceInterface interface. Currently only an ArrayPersistence implementation is available which uses a volatile array. You can write your own provider by implementing:

interface PersistenceInterface
{
    public function get($key);
    public function set($key, $value);
}

...and passing that into the constructor. Such as:

$breaker = new Breaker('testBreaker', new YourOwnPersistenceProvider);

Tests

To run all tests, use:

script/test

circuitbreaker's People

Contributors

bpscott avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

bbc stonelink

circuitbreaker's Issues

Stop failure count at threshold

Stop counting failures when the threshold is reached as the onClosed method will be returning false so the failure didn't really 'happen'...

Factory or POP constructor?

From review:

Currently the breaker uses a factory method to build a new instance.

$breaker = Breaker::build('testBreaker', new ArrayPersistence);

...but should it instead use a Plain Old PHP constructor like:

$breaker = new Breaker('testBreaker', new ArrayPersistence);

My reply:
The benefit of using a static 'build' method is that it can return an object of any subtype of the Breaker type and simplifies the interface to construct the Breaker itself. Also the static factory can potentially use caching or pooling to return an object without it having to be a new instance. Although the constructor is set to protected to provide the ability to sub-class the Breaker, should that be required.

A build method is another level of indirection, which is a disadvantage; but there's another level of flexibility too, which is an advantage.

In 'TDD-style' we should really be just using constructors and re-factoring when there starts to be issues—such as complex conditional logic in the object constructors—but as this is a library I'm making a punt that this is a better option in the long term; therefore picking it now will avoid the interface changing later.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.