Skip to content

Commit

Permalink
Remove factory class
Browse files Browse the repository at this point in the history
  • Loading branch information
stfndamjanovic committed Jan 18, 2024
1 parent d568060 commit 43aaa34
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 123 deletions.
134 changes: 121 additions & 13 deletions src/CircuitBreaker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,44 @@

class CircuitBreaker
{
public string $name;
/**
* @var string
*/
protected string $name;

/**
* @var Config
*/
public Config $config;
protected Config $config;

/**
* @var CircuitBreakerStorage
*/
public CircuitBreakerStorage $storage;
protected CircuitBreakerStorage $storage;

/**
* @var CircuitBreakerListener[]
*/
public array $listeners = [];
protected array $listeners = [];

/**
* @var \Closure|null
*/
public \Closure|null $failWhenCallback = null;
protected \Closure|null $failWhenCallback = null;

/**
* @var \Closure|null
*/
public \Closure|null $skipFailureCallback = null;
protected \Closure|null $skipFailureCallback = null;

/**
* @param Config|null $config
* @param CircuitBreakerStorage|null $storage
* @param string $name
*/
public function __construct(string $name, Config $config = null, CircuitBreakerStorage $storage = null)
private function __construct(string $name)
{
$this->name = $name;
$this->config = $config ?: new Config();
$this->storage = $storage ?: new InMemoryStorage();
$this->config = new Config();
$this->storage = new InMemoryStorage();
}

/**
Expand Down Expand Up @@ -121,10 +124,115 @@ public function addListener(CircuitBreakerListener $listener)

/**
* @param string $service
* @return CircuitBreakerFactory
* @return self
*/
public static function for(string $service)
{
return new CircuitBreakerFactory(new self($service));
return new self($service);
}

/**
* @param array $options
* @return $this
*/
public function withOptions(array $options): self
{
$this->config = Config::make($options);

return $this;
}

/**
* @param array $listeners
* @return $this
*/
public function withListeners(array $listeners): self
{
foreach ($listeners as $listener) {
$this->addListener($listener);
}

return $this;
}

/**
* @param \Closure $closure
* @return $this
*/
public function skipFailure(\Closure $closure)
{
$this->skipFailureCallback = $closure;

return $this;
}

/**
* @param \Closure $closure
* @return $this
*/
public function failWhen(\Closure $closure)
{
$this->failWhenCallback = $closure;

return $this;
}

/**
* @param CircuitBreakerStorage $storage
* @return $this
*/
public function storage(CircuitBreakerStorage $storage)
{
$this->storage = $storage;

return $this;
}

/**
* @return string
*/
public function getName(): string
{
return $this->name;
}

/**
* @return Config
*/
public function getConfig(): Config
{
return $this->config;
}

/**
* @return CircuitBreakerStorage
*/
public function getStorage(): CircuitBreakerStorage
{
return $this->storage;
}

/**
* @return array
*/
public function getListeners(): array
{
return $this->listeners;
}

/**
* @return \Closure|null
*/
public function getFailWhenCallback(): ?\Closure
{
return $this->failWhenCallback;
}

/**
* @return \Closure|null
*/
public function getSkipFailureCallback(): ?\Closure
{
return $this->skipFailureCallback;
}
}
69 changes: 0 additions & 69 deletions src/CircuitBreakerFactory.php

This file was deleted.

6 changes: 3 additions & 3 deletions src/StateHandlers/ClosedStateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ class ClosedStateHandler extends StateHandler
*/
public function onFailure(\Exception $exception)
{
$storage = $this->breaker->storage;
$storage = $this->breaker->getStorage();

$storage->incrementFailure();

$failuresCount = $storage->getFailuresCount();

if ($failuresCount >= $this->breaker->config->failureThreshold) {
if ($failuresCount >= $this->breaker->getConfig()->failureThreshold) {
$this->breaker->openCircuit();

throw CircuitOpenException::make($this->breaker->name);
throw CircuitOpenException::make($this->breaker->getName());
}
}
}
6 changes: 3 additions & 3 deletions src/StateHandlers/OpenStateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ class OpenStateHandler extends StateHandler
*/
public function beforeCall(\Closure $action, ...$args)
{
$storage = $this->breaker->storage;
$storage = $this->breaker->getStorage();

$openedAt = $storage->openedAt();

$recoveryTime = $this->breaker->config->recoveryTime;
$recoveryTime = $this->breaker->getConfig()->recoveryTime;

if ($openedAt && (time() - $openedAt) > $recoveryTime) {
$storage->setState(CircuitState::HalfOpen);

return;
}

throw CircuitOpenException::make($this->breaker->name);
throw CircuitOpenException::make($this->breaker->getName());
}
}
15 changes: 8 additions & 7 deletions src/StateHandlers/StateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public function __construct(CircuitBreaker $breaker)
/**
* @param \Closure $action
* @param ...$args
* @return mixed
* @return mixed|null
* @throws \Exception]
*/
public function call(\Closure $action, ...$args)
{
Expand Down Expand Up @@ -59,15 +60,15 @@ public function beforeCall(\Closure $action, ...$args)
*/
public function handleFailure(\Exception $exception)
{
if (is_callable($this->breaker->skipFailureCallback)) {
$shouldSkip = call_user_func($this->breaker->skipFailureCallback, $exception);
if (is_callable($this->breaker->getSkipFailureCallback())) {
$shouldSkip = call_user_func($this->breaker->getSkipFailureCallback(), $exception);

if ($shouldSkip) {
return;
}
}

foreach ($this->breaker->listeners as $listener) {
foreach ($this->breaker->getListeners() as $listener) {
$listener->onFail($exception);
}

Expand All @@ -83,8 +84,8 @@ public function handleFailure(\Exception $exception)
*/
public function handleSucess($result)
{
if (is_callable($this->breaker->failWhenCallback)) {
$shouldFail = call_user_func($this->breaker->failWhenCallback, $result);
if (is_callable($this->breaker->getFailWhenCallback())) {
$shouldFail = call_user_func($this->breaker->getFailWhenCallback(), $result);

if ($shouldFail) {
throw FailOnSuccessException::make();
Expand All @@ -93,7 +94,7 @@ public function handleSucess($result)

$this->onSucess();

foreach ($this->breaker->listeners as $listener) {
foreach ($this->breaker->getListeners() as $listener) {
$listener->onSuccess($result);
}
}
Expand Down
Loading

0 comments on commit 43aaa34

Please sign in to comment.