{"id":15018907,"url":"https://github.com/prestashop/circuit-breaker","last_synced_at":"2025-04-04T08:06:22.840Z","repository":{"id":52710081,"uuid":"160704651","full_name":"PrestaShop/circuit-breaker","owner":"PrestaShop","description":"A resilient PHP library that allows to safely make calls to external services","archived":false,"fork":false,"pushed_at":"2024-11-21T10:25:38.000Z","size":239,"stargazers_count":31,"open_issues_count":0,"forks_count":12,"subscribers_count":18,"default_branch":"develop","last_synced_at":"2025-04-04T07:51:22.293Z","etag":null,"topics":["circuit-breaker","guzzle","hacktoberfest","library","php","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/PrestaShop.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-12-06T16:52:12.000Z","updated_at":"2024-11-21T10:04:13.000Z","dependencies_parsed_at":"2023-11-20T10:36:55.378Z","dependency_job_id":"418d0a92-c6a0-4ca7-8788-2b88d0b7a9d0","html_url":"https://github.com/PrestaShop/circuit-breaker","commit_stats":{"total_commits":115,"total_committers":9,"mean_commits":"12.777777777777779","dds":0.7391304347826086,"last_synced_commit":"1ff44e3bd7e19573e6825dbb34e5042ce96f0225"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fcircuit-breaker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fcircuit-breaker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fcircuit-breaker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/PrestaShop%2Fcircuit-breaker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/PrestaShop","download_url":"https://codeload.github.com/PrestaShop/circuit-breaker/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247142063,"owners_count":20890652,"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","guzzle","hacktoberfest","library","php","resiliency"],"created_at":"2024-09-24T19:52:36.406Z","updated_at":"2025-04-04T08:06:22.824Z","avatar_url":"https://github.com/PrestaShop.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Circuit Breaker, an implementation for resilient PHP applications\n\n[![codecov](https://codecov.io/gh/PrestaShop/circuit-breaker/branch/master/graph/badge.svg)](https://codecov.io/gh/PrestaShop/circuit-breaker)\n[![PHPStan](https://img.shields.io/badge/PHPStan-Level%207-brightgreen.svg?style=flat\u0026logo=php)](https://shields.io/#/)\n[![Psalm](https://img.shields.io/badge/Psalm-Level%20Max-brightgreen.svg?style=flat\u0026logo=php)](https://shields.io/#/)\n[![Build](https://github.com/PrestaShop/circuit-breaker/actions/workflows/php.yml/badge.svg)](https://github.com/PrestaShop/circuit-breaker/actions/workflows/php.yml)\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 prestashop/circuit-breaker\n```\n\n## Use\n\n### Symfony Http Client and Guzzle Client implementations\n\nBy default, Circuit Breaker use the Symfony Http Client library, and all the client options are described in the [official documentation](https://symfony.com/doc/current/http_client.html).\n\nFor retro-compatibility, we let you use Guzzle Client instead of Symfony Http Client. To use Guzzle, you need to set the Guzzle client with `setClient()` of the settings factory, like this example below:\n\n```php\nuse PrestaShop\\CircuitBreaker\\SimpleCircuitBreakerFactory;\nuse PrestaShop\\CircuitBreaker\\FactorySettings;\nuse PrestaShop\\CircuitBreaker\\Client\\GuzzleClient\n\n$circuitBreakerFactory = new SimpleCircuitBreakerFactory();\n$factorySettings = new FactorySettings(2, 0.1, 10);\n$factorySettings-\u003esetClient(new GuzzleHttpClient());\n\n$circuitBreaker = $circuitBreakerFactory-\u003ecreate($factorySettings);\n```\n\nBe aware, that the client options depend on the client implementation you choose!\n \n\u003e For the Guzzle implementation, the Client options are described\n\u003e in the [HttpGuzzle documentation](http://docs.guzzlephp.org/en/stable/index.html).\n\n### Simple Circuit Breaker\n\nYou can use the factory to create a simple circuit breaker.\n\nBy default, you need to define 3 parameters for the circuit breaker:\n\n* the **failures**: define how many times we try to access the service;\n* the **timeout**: define how much time we wait before consider the service unreachable;\n* the **threshold**: define how much time we wait before trying to access again the service (once it is considered unreachable);\n\nThe **fallback** callback will be used if the distant service is unreachable when the Circuit Breaker is Open (means \"is used\" if the service is unreachable). \n\n\u003e You'd better return the same type of response expected from your distant call.\n\n```php\nuse PrestaShop\\CircuitBreaker\\SimpleCircuitBreakerFactory;\nuse PrestaShop\\CircuitBreaker\\FactorySettings;\n\n$circuitBreakerFactory = new SimpleCircuitBreakerFactory();\n$circuitBreaker = $circuitBreakerFactory-\u003ecreate(new FactorySettings(2, 0.1, 10));\n\n$fallbackResponse = function () {\n    return '{}';\n};\n\n$response = $circuitBreaker-\u003ecall('https://api.domain.com', [], $fallbackResponse);\n```\n\nIf you don't specify any fallback, by default the circuit breaker will return an empty string.\n\n```php\nuse PrestaShop\\CircuitBreaker\\SimpleCircuitBreakerFactory;\nuse PrestaShop\\CircuitBreaker\\FactorySettings;\n\n$circuitBreakerFactory = new SimpleCircuitBreakerFactory();\n$circuitBreaker = $circuitBreakerFactory-\u003ecreate(new FactorySettings(2, 0.1, 10));\n$response = $circuitBreaker-\u003ecall('https://unreacheable.api.domain.com', []); // $response == ''\n```\n\nYou can also define the client options (or even set your own client if you prefer).\n\n```php\nuse PrestaShop\\CircuitBreaker\\SimpleCircuitBreakerFactory;\nuse PrestaShop\\CircuitBreaker\\FactorySettings;\n\n$circuitBreakerFactory = new SimpleCircuitBreakerFactory();\n$settings = new FactorySettings(2, 0.1, 10);\n$settings-\u003esetClientOptions(['method' =\u003e 'POST']);\n$circuitBreaker = $circuitBreakerFactory-\u003ecreate($settings);\n$response = $circuitBreaker-\u003ecall('https://api.domain.com/create/user', ['body' =\u003e ['firstname' =\u003e 'John', 'lastname' =\u003e 'Doe']]);\n```\n\n### Advanced Circuit Breaker\n\nIf you need more control on your circuit breaker, you should use the `AdvancedCircuitBreaker` which manages more features:\n\n* the **stripped failures**: define how many times we try to access the service when the circuit breaker is Half Open (when it retires to reach the service after it was unreachable);\n* the **stripped timeout**: define how much time we wait before consider the service unreachable (again in Half open state);\n* the **storage**: used to store the circuit breaker states and transitions. By default it's an `SimpleArray` so if you want to \"cache\" the fact that your service is unreachable you should use a persistent storage;\n* the **transition dispatcher**: used if you need to subscribe to transition events (ex: a dispatcher based on Symfony EventDispatcher is available)\n\n#### Storage\n\n```php\nuse Doctrine\\Common\\Cache\\FilesystemCache;\nuse PrestaShop\\CircuitBreaker\\AdvancedCircuitBreakerFactory;\nuse PrestaShop\\CircuitBreaker\\FactorySettings;\nuse PrestaShop\\CircuitBreaker\\Storage\\DoctrineCache;\n\n$circuitBreakerFactory = new AdvancedCircuitBreakerFactory();\n$settings = new FactorySettings(2, 0.1, 60); //60 seconds threshold\n\n//Once the circuit breaker is open, the fallback response will be returned instantly during the next 60 seconds\n//Since the state is persisted even other requests/processes will be aware that the circuit breaker is open\n$doctrineCache = new FilesystemCache(_PS_CACHE_DIR_ . '/addons_category');\n$storage = new DoctrineCache($doctrineCache);\n$settings-\u003esetStorage($storage);\n\n$circuitBreaker = $circuitBreakerFactory-\u003ecreate($settings);\n$response = $circuitBreaker-\u003ecall('https://unreachable.api.domain.com/create/user', []);\n```\n\n## Tests\n\n```\ncomposer test\n```\n\n## Code quality\n\n```\ncomposer cs-fix \u0026\u0026 composer phpcb \u0026\u0026 composer psalm \u0026\u0026 composer phpcs\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\nIf you want to use it (using Docker):\n\n```\ndocker run --rm -u $UID -v $(pwd):/app eko3alpha/docker-phpqa --report --ignoredDirs vendor,tests\n```\n\nIf you want to use it (using Composer):\n\n```\ncomposer global require edgedesign/phpqa=v1.20.0 --update-no-dev\nphpqa --report --ignoredDirs vendor,tests\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestashop%2Fcircuit-breaker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprestashop%2Fcircuit-breaker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprestashop%2Fcircuit-breaker/lists"}