Skip to content

jolelievre/circuit-breaker

 
 

Repository files navigation

Circuit Breaker, an implementation for resilient PHP applications

codecov PHPStan Psalm Build Status

Main principles

circuit breaker

This library is compatible with PHP 5.6+.

Installation

composer require prestashop/circuit-breaker

Use

You can use the factory to create a simple circuit breaker.

By default, you need to define 3 parameters for each "state/place" of the circuit breaker:

  • the failures: define how much times we try to access the service;
  • the timeout: define how much time we wait before consider the service unreachable;
  • the threshold: define how much time we wait before trying to access again the service;

We also need to define which (HTTP) client will be used to call the service.

The fallback callback will be used if the distant service is unreachable when the Circuit Breaker is Open (means "is used").

You'd better return the same type of response expected from your distant call.

use PrestaShop\CircuitBreaker\SimpleCircuitBreakerFactory;

$circuitBreakerFactory = new SimpleCircuitBreakerFactory();
$circuitBreaker = $circuitBreakerFactory->create(
    [
        'closed' => [2, 0.1, 0],
        'open' => [0, 0, 10],
        'half_open' => [1, 0.2, 0],
        'client' => [
            'proxy' => '192.168.16.1:10',
            'method' => 'POST',
        ],
    ]
);

$fallbackResponse = function () {
    return '{}';
};

$circuitBreaker->call('https://api.domain.com', $fallbackResponse);

For the Guzzle implementation, the Client options are described in the HttpGuzzle documentation.

Tests

composer test

Code quality

composer cs-fix && composer phpstan

We also use PHPQA to check the Code quality during the CI management of the contributions.

If you want to use it (using Docker):

docker run --rm -u $UID -v $(pwd):/app eko3alpha/docker-phpqa --report --ignoredDirs vendor

If you want to use it (using Composer):

composer global require edgedesign/phpqa=v1.20.0 --update-no-dev
phpqa --report --ignoredDirs vendor

About

A resilient PHP library that allows to safely make calls to external services

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 98.9%
  • HCL 1.1%