Git Product home page Git Product logo

circuit-breaker-php's Introduction

PHP implementation of Circuit Breaker Pattern

Build Status Scrutinizer Code Quality Code Intelligence Status Total Downloads

For more information about this pattern see this.

This implementation has only redis adapter yet

Starting with composer

composer require leocarmo/circuit-breaker-php

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;

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

$adapter = new \LeoCarmo\CircuitBreaker\Adapters\RedisAdapter($redis, 'my-product');

// Set redis adapter for CB
CircuitBreaker::setAdapter($adapter);

Set circuit break settings

This is not required, default values ​​will be set

// Configure settings for CB  
CircuitBreaker::setGlobalSettings([  
  'timeWindow' => 60, // Time for an open circuit (seconds)  
  'failureRateThreshold' => 50, // Fail rate for open the circuit  
  'intervalToHalfOpen' => 30, // Half open time (seconds)  
]);

Configure settings for specific service

// Configure settings for specific service
CircuitBreaker::setServiceSettings('my-custom-service', [  
  'timeWindow' => 30, // Time for an open circuit (seconds)  
  'failureRateThreshold' => 15, // Fail rate for open the circuit  
  'intervalToHalfOpen' => 10, // Half open time (seconds)  
]);

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: `my-service`
if (! CircuitBreaker::isAvailable('my-service')) {  
  die('Circuit is not available!');  
}

Record success and failure

// Usage example for success and failure  
try {  
  Service::execute('something');  
  CircuitBreaker::success('my-service');  
} catch (\ServiceException $e) {  
  CircuitBreaker::failure('my-service');  
  die($e->getMessage());  
}

Credits

circuit-breaker-php's People

Contributors

afraprg avatar leocarmo avatar

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.