Skip to content

Commit

Permalink
Simplify circuit breaker configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
stfndamjanovic committed Jan 24, 2024
1 parent 07b5ece commit 5e5778b
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 232 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ use Stfn\CircuitBreaker\CircuitBreaker;

$breaker = CircuitBreaker::for('3rd-party-service')
->withOptions([
'failure_ratio' => 0.5,
'minimum_throughput' => 10,
'recovery_time' => 60,
'sample_duration' => 120
'failure_threshold' => 10,
'recovery_time' => 120,
'sample_duration' => 60,
]);
```

Expand Down
26 changes: 6 additions & 20 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,7 @@ class Config
/**
* @var float
*/
public float $failureRatio;

/**
* @var int
*/
public int $minimumThroughput;
public float $failureThreshold;

/**
* @var int
Expand All @@ -27,23 +22,16 @@ class Config
public int $sampleDuration;

/**
* @param float $failureRatio
* @param int $minimumThroughput
* @param float $failureThreshold
* @param int $recoveryTime
* @param int $sampleDuration
*/
public function __construct(
float $failureRatio = 0.5,
int $minimumThroughput = 10,
float $failureThreshold = 5,
int $recoveryTime = 60,
int $sampleDuration = 120
) {
if ($failureRatio < 0 || $failureRatio > 1) {
throw new \InvalidArgumentException("Failure ratio must be between 0 and 1");
}

$this->failureRatio = $failureRatio;
$this->minimumThroughput = $minimumThroughput;
$this->failureThreshold = $failureThreshold;
$this->recoveryTime = $recoveryTime;
$this->sampleDuration = $sampleDuration;
}
Expand All @@ -55,8 +43,7 @@ public function __construct(
public static function fromArray(array $config = []): Config
{
return new Config(
$config['failure_ratio'] ?? 0.5,
$config['minimum_throughput'] ?? 10,
$config['failure_threshold'] ?? 5,
$config['recovery_time'] ?? 60,
$config['sample_duration'] ?? 120
);
Expand All @@ -68,8 +55,7 @@ public static function fromArray(array $config = []): Config
public function toArray()
{
return [
'failure_ratio' => $this->failureRatio,
'minimum_throughput' => $this->minimumThroughput,
'failure_ratio' => $this->failureThreshold,
'recovery_time' => $this->recoveryTime,
'sample_duration' => $this->sampleDuration,
];
Expand Down
58 changes: 0 additions & 58 deletions src/Counter.php

This file was deleted.

12 changes: 1 addition & 11 deletions src/StateHandlers/ClosedStateHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@

class ClosedStateHandler extends StateHandler
{
/**
* @return void
*/
public function onSucess()
{
$this->breaker->getStorage()->incrementSuccess();
}

/**
* @param \Exception $exception
* @return void
Expand All @@ -27,9 +19,7 @@ public function onFailure(\Exception $exception)

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

$counter = $storage->getCounter();

if ($counter->total() >= $config->minimumThroughput && $counter->failureRatio() >= $config->failureRatio) {
if ($storage->getNumberOfFailures() >= $config->failureThreshold) {
$this->breaker->openCircuit();

throw CircuitOpenException::make($this->breaker->getName());
Expand Down
9 changes: 2 additions & 7 deletions src/Storage/CircuitBreakerStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,15 @@ abstract public function close(): void;
*/
abstract public function incrementFailure(): void;

/**
* @return void
*/
abstract public function incrementSuccess(): void;

/**
* @return void
*/
abstract public function resetCounter(): void;

/**
* @return Counter
* @return int
*/
abstract public function getCounter(): Counter;
abstract public function getNumberOfFailures(): int;

/**
* @return int
Expand Down
20 changes: 3 additions & 17 deletions src/Storage/InMemoryStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Stfn\CircuitBreaker\Storage;

use Stfn\CircuitBreaker\CircuitState;
use Stfn\CircuitBreaker\Counter;

class InMemoryStorage extends CircuitBreakerStorage
{
Expand All @@ -17,11 +16,6 @@ class InMemoryStorage extends CircuitBreakerStorage
*/
protected int $failCount = 0;

/**
* @var int
*/
protected int $successCount = 0;

/**
* @var int|null
*/
Expand Down Expand Up @@ -52,14 +46,6 @@ public function incrementFailure(): void
$this->failCount++;
}

/**
* @return void
*/
public function incrementSuccess(): void
{
$this->successCount++;
}

/**
* @return void
*/
Expand All @@ -69,11 +55,11 @@ public function resetCounter(): void
}

/**
* @return Counter
* @return int
*/
public function getCounter(): Counter
public function getNumberOfFailures(): int
{
return new Counter($this->failCount, $this->successCount);
return $this->failCount;
}

/**
Expand Down
24 changes: 7 additions & 17 deletions src/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class RedisStorage extends CircuitBreakerStorage
public const BASE_NAMESPACE = "stfn_php_circuit_breaker";
public const STATE_KEY = "state";
public const FAIL_COUNT_KEY = "fail_count";
public const SUCCESS_COUNT_KEY = "success_count";
public const OPENED_AT_KEY = "opened_at";

/**
Expand Down Expand Up @@ -72,16 +71,10 @@ public function setState(CircuitState $state): void
*/
public function incrementFailure(): void
{
$this->incrementOrCreate($this->getNamespace(self::FAIL_COUNT_KEY), $this->breaker->getConfig()->sampleDuration);
}

/**
* @return void
* @throws \RedisException
*/
public function incrementSuccess(): void
{
$this->incrementOrCreate($this->getNamespace(self::SUCCESS_COUNT_KEY), $this->breaker->getConfig()->sampleDuration);
$this->incrementOrCreate(
$this->getNamespace(self::FAIL_COUNT_KEY),
$this->breaker->getConfig()->sampleDuration
);
}

/**
Expand Down Expand Up @@ -110,15 +103,12 @@ public function resetCounter(): void
}

/**
* @return Counter
* @return int
* @throws \RedisException
*/
public function getCounter(): Counter
public function getNumberOfFailures(): int
{
$failuresCount = (int) $this->redis->get($this->getNamespace(self::FAIL_COUNT_KEY));
$successCount = (int) $this->redis->get($this->getNamespace(self::SUCCESS_COUNT_KEY));

return new Counter($failuresCount, $successCount);
return (int) $this->redis->get($this->getNamespace(self::FAIL_COUNT_KEY));
}

/**
Expand Down
Loading

0 comments on commit 5e5778b

Please sign in to comment.