A modern PHP library that allows you to make resilient calls to external services

Overview

Resiliency, an implementation for resilient and modern PHP applications

codecov PHPStan Psalm Build Status

Main principles

circuit breaker

This library is compatible with PHP 7.4+.

Installation

composer require love-oss/resiliency

Use

You need to configure a system for the Circuit Breaker:

  • the failures: define how many times we try to access the service;
  • the timeout: define how long we wait (in ms) before consider the service unreachable;
  • the stripped timeout: define how long we wait (in ms) before consider the service unreachable, once we're in half open state;
  • the threshold: define how long we wait (in ms) before trying to access again the service;
  • the (HTTP|HTTPS) client that will be used to reach the services;
  • 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 Resiliency\MainCircuitBreaker;
use Resiliency\Systems\MainSystem;
use Resiliency\Storages\SimpleArray;
use Resiliency\Clients\SymfonyClient;
use Symfony\Component\HttpClient\HttpClient;

$client = new SymfonyClient(HttpClient::create());

$mainSystem = MainSystem::createFromArray([
    'failures' => 2,
    'timeout' => 100,
    'stripped_timeout' => 200,
    'threshold' => 10000,
], $client);

$storage = new SimpleArray();

// Any PSR-14 Event Dispatcher implementation.
$dispatcher = new Symfony\Component\EventDispatcher\EventDispatcher;

$circuitBreaker = new MainCircuitBreaker(
    $mainSystem,
    $storage,
    $dispatcher
);

/**
 * @var Service $service
 */
$fallbackResponse = function ($service) {
    return '{}';
};

$circuitBreaker->call(
    'https://api.domain.com',
    $fallbackResponse,
    [
        'query' => [
            '_token' => '123456789',
        ]
    ]
);

Clients

Resiliency library supports both Guzzle (v6 & v7) and HttpClient Component from Symfony (v4 & v5).

Monitoring

This library provides a minimalist system to help you monitor your circuits.

$monitor = new SimpleMonitor();

// Collect information while listening
// to some circuit breaker events...
function listener(Event $event) {
    $monitor->add($event);
};

// Retrieve a complete report for analysis or storage
$report = $monitor->getReport();

Tests

composer test

Code quality

This library has high quality standards:

composer cs-fix && composer phpstan && composer psalm && composer phpqa

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

composer phpqa

I've heard of the PrestaShop Circuit Breaker: what library should I use ?

Welcome, that's an interesting question !

Above all, I must say that I'm the former author of the PrestaShop Circuit Breaker library and I have decided to fork my own library to be able to improve it without the constraints of the PrestaShop CMS main project.

As of now (June, 2021), these libraries have a lot in common !

They share almost the same API, and the PrestaShop Core Team have created multiple implementations of Circuit Breaker interface and Factory :

  • SimpleCircuitBreaker
  • AdvancedCircuitBreaker
  • PartialCircuitBreaker
  • SymfonyCircuitBreaker
  1. They maintain a version compatible with PHP 7.2+ and Symfony 4 but not (yet ?) with PHP 8 and Symfony 5 ;
  2. They have a dependency on their own package named php-dev-tools ;
  3. They maintain an implementation of Storage using Doctrine Cache library ;
  4. They don't have a Symfony HttpClient implementation ;
  5. For the events, I'm not sure as their implementation make the list difficult to establish ;
  6. They don't provide a mecanism to reset and restore a Circuit Breaker ;
  7. They don't provide a mecanism to monitor the activity of a Circuit Breaker ;
  8. They have removed Psalm from their CI and they don't use PHPQA ;
  9. They have added declare(strict_types=1); on all the files ;
  10. They don't declare a .gitattributes file, this means that all tests are downloaded when we require their library ;

All right ... but this don't tell me what library should I use in my project !

  • If you need PHP 5.6, use Circuit Breaker v3
  • If you need PHP 7.2, use Circuit Breaker v4
  • If you need PHP 7.4+, use Resiliency
  • If you need a library maintained by a team of developers, use PrestaShop
  • If you trust me to maintain this package almost all alone, use Resiliency !
You might also like...
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Requests - a HTTP library written in PHP, for human beings

Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python library. Requests is ISC Licensed (similar to the new BSD license) and has no dependencies, except for PHP 5.6+.

Declarative HTTP Clients using Guzzle HTTP Library and PHP 8 Attributes

Waffler How to install? $ composer require waffler/waffler This package requires PHP 8 or above. How to test? $ composer phpunit Quick start For our e

Supercharge your app or SDK with a testing library specifically for Guzzle

Full Documentation at guzzler.dev Supercharge your app or SDK with a testing library specifically for Guzzle. Guzzler covers the process of setting up

LittleProxy is a high performance HTTP proxy written in Java atop Trustin Lee's excellent Netty event-based networking library

LittleProxy is a high performance HTTP proxy written in Java atop Trustin Lee's excellent Netty event-based networking library

The Library for HTTP Status Codes, Messages and Exception

This is a PHP library for HTTP status codes, messages and error exception. Within the library, HTTP status codes are available in classes based on the section they belong to. Click this link for more information.

A library that makes the management of WordPress file headers easier.

Pronamic WordPress File Header Many WordPress plugins contain bash scripts with sed and awk commands to update WordPress file headers. Because sed and

Retrofit implementation in PHP. A REST client for PHP.

Retrofit PHP Retrofit is a type-safe REST client. It is blatantly stolen from square/retrofit and implemented in PHP. ❗ UPGRADE NOTICE ❗ Version 3 int

HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7

HTTP header kit for PHP 7.1+ (incl. PHP 8) based on PSR-7 Installation composer require sunrise/http-header-kit How to use? HTTP Header Collection Mor

Comments
  • Introduced manual controls

    Introduced manual controls

    Introduced isolate and reset functions:

    • isolate a circuit breaker will make it rely on the fallback response until we reset it, it could be useful when we put an application on maintenance state;
    • reset put the circuit breaker on CLOSED state again, without remove any previous transaction: this means you won't trigger "INITIATING" again.

    About that, what would be the more logic behavior? /c @jolelievre

    opened by mickaelandrieu 1
  • Updated incoming circuit state on transaction retrieval

    Updated incoming circuit state on transaction retrieval

    Between 2 requests, the circuit breaker is not retrieved in the right place.

    Let's say, it was open and then we call again the service in a new request (so the circuit is retrieved from the storage), we should put the circuit breaker at the same place/state.

    opened by mickaelandrieu 1
  • Improved main system

    Improved main system

    • Added a new Helper on MainSystem class to ease the construction of each Circuit Breaker.
    • Refactored (BC) the circuit breakers
    • Revealed non executed testsuite! that is sad :/
    opened by mickaelandrieu 1
Releases(2.3)
  • 2.3(Jan 25, 2022)

    ROADMAP

    • https://github.com/loveOSS/resiliency/pull/62 Allow developers to get information about the exception in case of failure
    • https://github.com/loveOSS/resiliency/pull/64, https://github.com/loveOSS/resiliency/pull/65, https://github.com/loveOSS/resiliency/pull/66 : upgrade development dependencies

    And the support of Symfony 5.4 / 6.0

    Enjoy !

    Source code(tar.gz)
    Source code(zip)
  • 2.2(Sep 4, 2021)

    CHANGELOG

    • #54, #55, #56, #57 : Docs improvements, and also note on PrestaShop Circuit Breaker library
    • #59 : Fixed error on Timeout validation (ms to s)
    Source code(tar.gz)
    Source code(zip)
  • 2.1(Jun 4, 2021)

    CHANGELOG

    • https://github.com/loveOSS/resiliency/commit/d3155e4d55c2e5f3b236c89948b12d5da2de4d13 Upgraded and ensured the support of Symfony 5.3

    Thanks for all the support and stars !

    Source code(tar.gz)
    Source code(zip)
  • 2.0(Mar 30, 2021)

    CHANGELOG

    • The PHP minimal version is now PHP 7.4 and is compatible with PHP 8.0
    • The support of Symfony 3.4 have been removed in favor of Symfony 5.2+
    • Security Checker have been removed (unmaintained) and will be re-introduced soon
    • The Storage strategy using PSR-16 have been updated (Symfony direct implementation was deprecated, see https://github.com/loveOSS/resiliency/pull/46)

    The support of previous versions (1.x) is not guaranteed anymore, feel free to fork if you want a PHP 5.6 <-> 7.4 compatible version.

    Thanks for all the support and stars !

    Source code(tar.gz)
    Source code(zip)
  • 1.1(Mar 31, 2020)

    CHANGELOG

    c468e65 Fixed Symfony HttpClient query use 64e41ce Added a PHP timeout exception on System initialization 90bf299 Reduced the size of the future releases

    1.x branch will be maintained until end of 2020, new features will now merged on "soon-to-be" 2.0.

    Source code(tar.gz)
    Source code(zip)
  • 1.0(Sep 20, 2019)

    CHANGELOG

    • 4dd5226 The fallback now have access to the Service object;
    • 6be2cbc MainSystem class was refactored and tests added;
    • ac71241 The UnavailableServiceException class now has access to previous exception and code;
    • 411abb0 Introduced the Monitoring feature, take a look at the readme for an example;
    • 6820f1e PHP 7.3 support is now official, we will keep PHP 7.2 support until the end of 2020;
    • 9c1a93b Introduced a new adapter for Symfony 4.3 HttpClient component
    Source code(tar.gz)
    Source code(zip)
  • 0.5(Jul 28, 2019)

    CHANGELOG

    5e242cc Removed place suffixes d806d00 Implemented Psr Event Dispatcher ab1c84d Enabled Symfony Security Checker 4faa39a Extracted business logic into Places b8b65c3 Support of Symfony 4.3 0b0dfe1 More robust test suite

    We're almost ready for the first v1 release! Only #12 is a blocker at this date.

    Source code(tar.gz)
    Source code(zip)
  • 0.4(May 24, 2019)

    CHANGELOG

    Looking at the awesome C# Polly library, we have refactored the library to make the creation of a circuit breaker easier.

    We end up with only one implementation of the CircuitBreaker interface, and we have removed the annoying step of having to configure places behind the MainSystem class.

    This refactoring allowed us to implement 2 new methods for the Circuit Breaker:

    • isolate(string $service) that keeps the circuit open until we ...
    • reset(string $service) that close the circuit that was previously isolated from the external system

    See the complete changelog above:

    5449892 Refactored the System class d0096e3 && e9a7f37 && fcfae84 Added more tests, code coverage is near to 100% right now 0641fd4 Introduced CircuitBreaker::isolate($service) and CircuitBreaker::reset($service) functions

    Enjoy!

    Source code(tar.gz)
    Source code(zip)
  • 0.3(May 16, 2019)

    CHANGELOG

    8037e4c (bugfix) Invalid transition on HALF_OPEN state 5c57702 (bugfix) Invalid transition on Transaction retrieval 83ecc2f (improvement) Refactoring that make only one implementation of circuit breaker and adds a transition dispatcher

    Source code(tar.gz)
    Source code(zip)
  • 0.2.1(May 11, 2019)

  • 0.2(May 11, 2019)

  • 0.1(May 11, 2019)

Owner
We love open source softwares
We love open source softwares
Spike is a fast reverse proxy built on top of ReactPHP that helps to expose your local services to the internet.

Spike is a fast reverse proxy built on top of ReactPHP that helps to expose your local services to the internet. 简体中文 Installation Install via compose

Tao 649 Dec 26, 2022
Transform your WordPress site into a modern GraphQL server: graphql-api.com.

GraphQL API for WordPress Transform your WordPress site into a modern GraphQL server: graphql-api.com. This plugin is the implementation for WordPress

GraphQL API 151 Dec 14, 2022
librestful is a virion for PocketMine servers that make easier, readable code and for async http requests.

librestful is a virion for PocketMine servers that make easier, readable code for async rest requests.

RedMC Network 17 Oct 31, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.

Requests for PHP Requests is a HTTP library written in PHP, for human beings. It is roughly based on the API from the excellent Requests Python librar

null 3.5k Dec 31, 2022
Unirest in PHP: Simplified, lightweight HTTP client library.

Unirest for PHP Unirest is a set of lightweight HTTP libraries available in multiple languages, built and maintained by Mashape, who also maintain the

Kong 1.3k Dec 28, 2022
The best php curl library.

中文文档 About Implemented by using php-curl internal io event with high performance,high universality,high extensibility which especially suitable for ma

Ares 431 Dec 12, 2022
PHP Secure Communications Library

phpseclib - PHP Secure Communications Library Supporting phpseclib Become a backer or sponsor on Patreon One-time donation via PayPal or crypto-curren

null 4.9k Jan 3, 2023
Custom PHP curl library for the Laravel 5 framework - developed by Ixudra

ixudra/curl Custom PHP cURL library for the Laravel 4 or 5 framework - developed by Ixudra. The package provides an easy interface for sending cURL re

Jan Oris 556 Jan 6, 2023
This library provides an object-oriented wrapper of the PHP cURL extension

PHP Curl Class This library provides an object-oriented wrapper of the PHP cURL extension. If you have questions or problems with installation or usag

PHP MOD 321 Dec 30, 2022