{"id":13616670,"url":"https://github.com/loveOSS/resiliency","last_synced_at":"2025-04-14T03:31:19.177Z","repository":{"id":34408320,"uuid":"179379655","full_name":"loveOSS/resiliency","owner":"loveOSS","description":"A modern PHP library that allows you to make resilient calls to external services :repeat:","archived":true,"fork":false,"pushed_at":"2022-01-25T22:15:10.000Z","size":270,"stargazers_count":78,"open_issues_count":6,"forks_count":7,"subscribers_count":4,"default_branch":"main","last_synced_at":"2024-04-26T09:03:19.984Z","etag":null,"topics":["circuit-breaker","library","php74","php8","quality","resiliency"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loveOSS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-03T22:28:45.000Z","updated_at":"2023-11-09T19:09:15.000Z","dependencies_parsed_at":"2022-08-08T01:00:31.669Z","dependency_job_id":null,"html_url":"https://github.com/loveOSS/resiliency","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loveOSS%2Fresiliency","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loveOSS%2Fresiliency/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loveOSS%2Fresiliency/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loveOSS%2Fresiliency/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loveOSS","download_url":"https://codeload.github.com/loveOSS/resiliency/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248298351,"owners_count":21080319,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["circuit-breaker","library","php74","php8","quality","resiliency"],"created_at":"2024-08-01T20:01:31.730Z","updated_at":"2025-04-14T03:31:18.806Z","avatar_url":"https://github.com/loveOSS.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Resiliency, an implementation for resilient and modern PHP applications\n\n[![codecov](https://codecov.io/gh/loveOSS/resiliency/branch/master/graph/badge.svg)](https://codecov.io/gh/loveOSS/resiliency) [![PHPStan](https://img.shields.io/badge/PHPStan-Level%20max-brightgreen.svg?style=flat\u0026logo=php)](https://shields.io/#/) [![Psalm](https://img.shields.io/badge/Psalm-Level%20Max-brightgreen.svg?style=flat\u0026logo=php)](https://shields.io/#/) [![Build Status](https://travis-ci.com/loveOSS/resiliency.svg?branch=master)](https://travis-ci.com/loveOSS/resiliency) \n\n## Main principles\n\n![circuit breaker](https://user-images.githubusercontent.com/1247388/49721725-438bd700-fc63-11e8-8498-82ca681b15fb.png)\n\nThis library is compatible with PHP 7.4+.\n\n## Installation\n\n```\ncomposer require love-oss/resiliency\n```\n\n## Use\n\nYou need to configure a system for the Circuit Breaker:\n\n* the **failures**: define how many times we try to access the service;\n* the **timeout**: define how long we wait (in ms) before consider the service unreachable;\n* the **stripped timeout**: define how long we wait (in ms) before consider the service unreachable, once we're in half open state;\n* the **threshold**: define how long we wait (in ms) before trying to access again the service;\n* the (HTTP|HTTPS) **client** that will be used to reach the services;\n* the **fallback** callback will be used if the distant service is unreachable when the Circuit Breaker is Open (means \"is used\"). \n\n\u003e You'd better return the same type of response expected from your distant call.\n\n```php\nuse Resiliency\\MainCircuitBreaker;\nuse Resiliency\\Systems\\MainSystem;\nuse Resiliency\\Storages\\SimpleArray;\nuse Resiliency\\Clients\\SymfonyClient;\nuse Symfony\\Component\\HttpClient\\HttpClient;\n\n$client = new SymfonyClient(HttpClient::create());\n\n$mainSystem = MainSystem::createFromArray([\n    'failures' =\u003e 2,\n    'timeout' =\u003e 100,\n    'stripped_timeout' =\u003e 200,\n    'threshold' =\u003e 10000,\n], $client);\n\n$storage = new SimpleArray();\n\n// Any PSR-14 Event Dispatcher implementation.\n$dispatcher = new Symfony\\Component\\EventDispatcher\\EventDispatcher;\n\n$circuitBreaker = new MainCircuitBreaker(\n    $mainSystem,\n    $storage,\n    $dispatcher\n);\n\n/**\n * @var Service $service\n */\n$fallbackResponse = function ($service) {\n    return '{}';\n};\n\n$circuitBreaker-\u003ecall(\n    'https://api.domain.com',\n    $fallbackResponse,\n    [\n        'query' =\u003e [\n            '_token' =\u003e '123456789',\n        ]\n    ]\n);\n```\n\n### Clients\n\nResiliency library supports both [Guzzle (v6 \u0026 v7)](http://docs.guzzlephp.org/en/stable/index.html) and HttpClient Component from [Symfony (v4 \u0026 v5)](https://symfony.com/doc/current/components/http_client.html).\n\n### Monitoring\n\nThis library provides a minimalist system to help you monitor your circuits.\n\n```php\n$monitor = new SimpleMonitor();\n\n// Collect information while listening\n// to some circuit breaker events...\nfunction listener(Event $event) {\n    $monitor-\u003eadd($event);\n};\n\n// Retrieve a complete report for analysis or storage\n$report = $monitor-\u003egetReport();\n```\n\n## Tests\n\n```\ncomposer test\n```\n\n## Code quality\n\nThis library has high quality standards:\n\n```\ncomposer cs-fix \u0026\u0026 composer phpstan \u0026\u0026 composer psalm \u0026\u0026 composer phpqa\n```\n\nWe also use [PHPQA](https://github.com/EdgedesignCZ/phpqa#phpqa) to check the Code quality\nduring the CI management of the contributions:\n\n```\ncomposer phpqa\n```\n\n ## I've heard of the PrestaShop Circuit Breaker: what library should I use ?\n \n Welcome, that's an interesting question !\n \n Above all, I must say that I'm the former author of the PrestaShop [Circuit Breaker](https://github.com/PrestaShop/circuit-breaker) library\n and I have decided to fork my own library to be able to improve it without the constraints of the PrestaShop CMS main project.\n \n As of now (June, 2021), these libraries have a lot in common !\n \nThey share almost the same API, and the PrestaShop Core Team have created multiple implementations of [Circuit Breaker interface](https://github.com/PrestaShop/circuit-breaker/blob/develop/src/AdvancedCircuitBreaker.php) and [Factory](https://github.com/PrestaShop/circuit-breaker/blob/develop/src/AdvancedCircuitBreakerFactory.php) :\n\n* SimpleCircuitBreaker\n* AdvancedCircuitBreaker\n* PartialCircuitBreaker\n* SymfonyCircuitBreaker\n\n\n1. They maintain a version compatible with PHP 7.2+ and Symfony 4 but not (yet ?) with PHP 8 and Symfony 5 ;\n2. They have a dependency on their own package named [php-dev-tools](https://github.com/PrestaShop/php-dev-tools) ;\n3. They maintain an implementation of [Storage](https://github.com/PrestaShop/circuit-breaker/blob/v4.0.0/src/Storage/DoctrineCache.php) using Doctrine Cache library ;\n4. They don't have a Symfony HttpClient implementation ;\n5. For the events, I'm not sure as their implementation make the list difficult to establish ;\n6. They don't provide a mecanism to reset and restore a Circuit Breaker ;\n7. They don't provide a mecanism to monitor the activity of a Circuit Breaker ;\n8. They have removed [Psalm](https://psalm.dev/) from their CI and they don't use [PHPQA](https://github.com/EdgedesignCZ/phpqa) ;\n9. They have added `declare(strict_types=1);` on all the files ;\n10. They don't declare a `.gitattributes` file, this means that all tests are downloaded when [we require](https://madewithlove.com/blog/software-engineering/gitattributes/) their library ;\n\n\u003e All right ... but this don't tell me what library should I use in my project !\n\n* If you need PHP 5.6, use Circuit Breaker v3\n* If you need PHP 7.2, use Circuit Breaker v4\n* If you need PHP 7.4+, use Resiliency\n* If you need a library maintained by a team of developers, use PrestaShop\n* If you trust [me](https://github.com/mickaelandrieu) to maintain this package _almost_ all alone, use Resiliency !\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FloveOSS%2Fresiliency","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FloveOSS%2Fresiliency","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FloveOSS%2Fresiliency/lists"}