Download the PHP package gabrielanhaia/php-circuit-breaker without Composer

On this page you can find all versions of the php package gabrielanhaia/php-circuit-breaker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package php-circuit-breaker

Build Status Code Coverage Licence Package Stars

PHP Circuit Breaker

Description

PHP Circuit Breaker was developed based on the book "Release It!: Design and Deploy Production-Ready Software (Pragmatic Programmers)", written by Michael T. Nygard. In this book, Michael popularized the Circuit Breaker.

When we work with microservices, it is sometimes common to call these systems, and they are not available, which ends up causing problems in our application. To prevent any problem on our side, and guarantee that a service will not be called loads of times, we should use a Circuit Breaker.

You can find more information about Circuit Breakers here.

Requirements


Installation

You can install PHP Circuit Breaker by composer running:


How do I use it?

I strongly recommend that you use a service container (dependency injection container) to deal with the objects and their dependencies, so you will be able to use it efficiently (It will not be necessary to create instances everywhere).

  1. The first thing you can do is to define the Settings:

Note: It is not necessary to define these settings (they are the default values), they will be defined automatically.

  1. Instantiating a driver (Only Redis driver is available at the moment) and Redis client:

  2. Instantiating the PHP Circuit Breaker class:

Note: The second parameter is optional.

  1. Validating if the circuit is open:

You can use the function canPass in any way you want. It will always return true when the Circuit is CLOSED or HALF_OPEN. After that, you should call your service, and depending on the response, you can call the following methods to update the circuit control variables.

If Success:

If failure:

With these three simple methods, you can control the flow of your application in execution time.


Recap

Let's say that you are using the following settings:

One of your services is a Payment Gateway, and you try to call it in an interval of each 2 seconds for some reason. The first time you call the Gateway, it responds with a 200 (HTTP status code), and after you call the method "succeed" with a service identifier (You can create one for each service).

On the second, third, fourth, fifth, and sixth call, the Gateway is unavailable, so you call the method "failed" again.

The total of failers was 5, now the next time you call the method "canPass" it will return "false" and the service will not be called again. At this moment the circuit is open, it will stay "OPEN" for 30 seconds (time_out_open), and then it will change the state to "HALF_OPEN" at this moment you can try to call the service again, and if it fails it will be "OPEN" for more 30 seconds.

What happens if the first four attempts fail and the fifth is succeeded? Then, the counter will be reset.

What is the setting "time_window" for? Each failure is stored on Redis and has an expiration date. If the first failure happened exaclty at 12:00:10 and the "time_window" is 30 seconds, so, after 12:00:40 this failure will not be counted in the total of failures considered to open the circuit. In short, to open the circuit, you must have X (total_failures) in an interval of Y (time_window) seconds.


Created by: Gabriel Anhaia - https://www.linkedin.com/in/gabrielanhaia


All versions of php-circuit-breaker with dependencies

PHP Build Version
Package Version
Requires ext-redis Version *
mockery/mockery Version ^1.4
eloquent/enumeration Version ^6.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package gabrielanhaia/php-circuit-breaker contains the following files

Loading the files please wait ....