Skip to content

Commit

Permalink
Add circuit breaker factory
Browse files Browse the repository at this point in the history
  • Loading branch information
stfndamjanovic committed Jan 10, 2024
1 parent 60f3cec commit 7df379a
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 14 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ composer require stfndamjanovic/php-circuit-breaker
## Usage

```php
use Stfn\CircuitBreaker\CircuitBreaker;
use Stfn\CircuitBreaker\Storage\RedisStorage;
use Redis;
use Stfn\CircuitBreaker\Config;

$breaker = new CircuitBreaker();

$breaker->call(function () {
// Your function that could fail
});
use Stfn\CircuitBreaker\CircuitBreakerFactory;

$result = CircuitBreakerFactory::make()
->for('test-service')
->withOptions([
'recovery_time' => 30,
'failure_threshold' => 5
])->call(function () {
// Your function that could fail
});
```

## Testing
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "stfndamjanovic/circuit-breaker",
"name": "stfn/circuit-breaker",
"description": "This is my package circuit-breaker",
"keywords": [
"stfndamjanovic",
Expand Down
3 changes: 2 additions & 1 deletion src/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Stfn\CircuitBreaker;

use Stfn\CircuitBreaker\Exceptions\InvalidStateException;
use Stfn\CircuitBreaker\StateHandlers\ClosedStateHandler;
use Stfn\CircuitBreaker\StateHandlers\HalfOpenStateHandler;
use Stfn\CircuitBreaker\StateHandlers\OpenStateHandler;
Expand Down Expand Up @@ -58,7 +59,7 @@ protected function makeStateHandler()
];

if (! array_key_exists($state->value, $map)) {
throw new \Exception("State {$state->value} is not valid");
throw InvalidStateException::make($state->value);
}

return new $map[$state->value]($this);
Expand Down
44 changes: 44 additions & 0 deletions src/CircuitBreakerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Stfn\CircuitBreaker;

use Stfn\CircuitBreaker\Storage\CircuitBreakerStorage;

class CircuitBreakerFactory
{
protected CircuitBreaker $circuitBreaker;

public static function make()
{
$object = new self();
$object->circuitBreaker = new CircuitBreaker();

return $object;
}

public function for(string $service)
{
$this->circuitBreaker->storage->setService($service);

return $this;
}

public function withOptions(array $options)
{
$this->circuitBreaker->config = Config::make($options);

return $this;
}

public function storage(CircuitBreakerStorage $storage)
{
$this->circuitBreaker->storage = $storage;

return $this;
}

public function call(\Closure $action, ...$args)
{
return $this->circuitBreaker->call($action, $args);
}
}
15 changes: 15 additions & 0 deletions src/Exceptions/InvalidStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Stfn\CircuitBreaker\Exceptions;

class InvalidStateException extends \Exception
{
/**
* @param $state
* @return InvalidStateException
*/
public static function make($state)
{
return new self("State {$state} is not valid");
}
}
9 changes: 9 additions & 0 deletions src/Storage/CircuitBreakerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ public function getService(): string
return $this->service;
}

/**
* @param string $service
* @return void
*/
public function setService(string $service)
{
$this->service = $service;
}

/**
* @return CircuitState
*/
Expand Down
10 changes: 8 additions & 2 deletions src/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,22 @@ class RedisStorage extends CircuitBreakerStorage
*/
protected \Redis $redis;

/**
* @var
*/
protected string $namespace;

/**
* @param string $service
* @param \Redis $redis
* @throws \RedisException
*/
public function __construct(string $service, \Redis $redis)
public function __construct(string $namespace, string $service, \Redis $redis)
{
parent::__construct($service);

$this->redis = $redis;
$this->namespace = $namespace;

$this->initState();
}
Expand Down Expand Up @@ -103,7 +109,7 @@ public function openedAt(): int
*/
protected function getNamespace(string $key): string
{
$tags = [self::BASE_NAMESPACE, $this->service, $key];
$tags = [self::BASE_NAMESPACE, $this->namespace, $this->service, $key];

return join(":", $tags);
}
Expand Down

0 comments on commit 7df379a

Please sign in to comment.