Giter VIP home page Giter VIP logo

php-circuit-breaker's Introduction

PHP Circuit Breaker

This is a POC library for a PHP fault tolerance library that is going to be built by Zendesk.

PHPCircuitBreaker is a fault tolerance library that can prevent cascading system failure for applications that rely heavily on remote services. This is done by throttling outgoing requests depending on the health of the remote service being called by effectively preventing consecutive request failures for any particular service.

PHPCircuitBreaker will stop requests made to an unhealthy service until such a point that it has found that the service is once again healthy enough to handle requests.

Installation

PHPCircuitBreaker can be installed using Composer

Add the following to your composer.json file

"repositories": [
  {
    "type": "git",
    "url": "[email protected]:zendesk/zendesk_laravel_diagnostic.git"
  }
],

Under require:

"zendesk/php_circuit_breaker": "dev-master"

And then run:

composer install zendesk/php_circuit_breaker

Usage

PHPCircuitBreaker protects remote service calls by allowing applications to wrap a function callback within an execute block. A minimal implementation to wrap outgoing guzzle requests would look like:

<?php

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

$guzzle = new GuzzleHttp\Client();

$fallback = function (\Exception $e) {
    echo $e->getMessage();
};

$storage = new Zendesk\PHPCircuitBreaker\RedisDataStore('127.0.0.1');
$circuit = new Zendesk\PHPCircuitBreaker\CircuitBreaker('guzzle_request', $storage, [], $fallback);

$circuit->execute(function() use ($guzzle) {
    return $guzzle->request('GET', 'https://api.github.com/user', [
        'auth' => ['user', 'pass']
    ]);
});

To keep the state of the circuit, PHPCircuitBreaker needs to be passed a data storage to hold information about the circuit and the services it is interacting with. This library ships with a Redis adapater to allow for easier adaption out of the box.

The CircuitBreaker object needs to be passed in

  • service
    • Value: String
    • The identifier/name of the service being protected
  • dataStore
    • Value: AbstractDataStore
    • Implementation of the AbstractDataStore to keep the state of the circuit
  • config
    • Value: Array
    • Configurable options for the circuit breaker
      • requestCountThreshold
        • Value: Integer
        • Number of requests before the circuit starts to evaluate the health of the remote service
      • allowedErrorPercentage
        • Value: Float
        • Percentage of errors at which the circuit breaker will open and stop any more outgoing requests
      • testWaitInMilliSeconds
        • Value: Integer
        • Number of milliseconds before trying to see if an open circuit can be closed
  • fallback
    • Value: function
    • A callback function to be called when the circuit breaker records a failed service request.

Testing

This project uses phpunit for unit testing.

After cloning, run composer install --dev to make sure you have all the dependencies. You can then run vendor/bin/phpunit tests to run the unit tests.

Useful Links

Project Structure

This project conforms to the PSR-2 PHP coding standard. To test your code against the standard, you can run vendor/bin/phpcs --extensions=php --standard=PSR2 -p src/ tests/

php-circuit-breaker's People

Contributors

samgavinio avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

jacques

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.