pyjac / opinmona
dev-master
2020-04-25 21:36 UTC
Requires
- symfony/cache: ^4.3
Requires (Dev)
- phpunit/phpunit: ^8
This package is auto-updated.
Last update: 2024-12-26 07:28:11 UTC
README
Opinmona is an implementation of Circuit Breaker in PHP.
Usage
Opinmona serves as a wrapper around methods or any callable you want to circuit break.
Usage for object method
use Pyjac\Opinmona\CircuitBreakerFactory; use Pyjac\Opinmona\CircuitBreakerOpenException; class TestApi { public function send(string $a) { return $a; } } $api = new TestApi(); $cb = CircuitBreakerFactory::fromMethod($api, 'send'); try { $cb->invoke(2); } catch(CircuitBreakerOpenException $e) { // }
Usage for callable class
use Pyjac\Opinmona\CircuitBreakerFactory; use Pyjac\Opinmona\CircuitBreakerOpenException; class TestApiCallable { public function __invoke(string $a) { return $a; } } $api = new TestApiCallable(); $cb = CircuitBreakerFactory::fromCallable($api); try { $cb->invoke(2); } catch(CircuitBreakerOpenException $e) { // }
Usage for function
use Pyjac\Opinmona\CircuitBreakerFactory; use Pyjac\Opinmona\CircuitBreakerOpenException; $testApiFunction = function (string $a) { return $a; }; $cb = CircuitBreakerFactory::fromCallable($testApiFunction); try { $cb->invoke('something'); } catch(CircuitBreakerOpenException $e) { // }
Updating options and Storage
use Pyjac\Opinmona\CircuitBreakerFactory; use Pyjac\Opinmona\CircuitBreakerOpenException; use Symfony\Component\Cache\Adapter\ArrayAdapter; class TestApiCallable { public function __invoke(string $a) { return $a; } } $options = [ 'ttl' => 5600, 'cachePrefix' => 'my-key', 'maxFailures' => 10, 'retryTimeoutInSeconds' => 2, ]; // Error Check function $errorCheck = function($response, $circuitObject) { return false; }; $api = new TestApiCallable(); $cb = CircuitBreakerFactory::fromCallable($api, $errorCheck, new ArrayAdapter(), $options); try { $cb->invoke(5) } catch(CircuitBreakerOpenException $e) { };