-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircuitBreaker.php
154 lines (129 loc) · 4.01 KB
/
CircuitBreaker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php declare(strict_types=1);
/*
* This file is part of ksaveras/circuit-breaker.
*
* (c) Ksaveras Sakys <xawiers@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Ksaveras\CircuitBreaker;
use Ksaveras\CircuitBreaker\Exception\OpenCircuitException;
use Ksaveras\CircuitBreaker\HeaderPolicy\HttpHeaderPolicy;
use Ksaveras\CircuitBreaker\HeaderPolicy\PolicyChain;
use Ksaveras\CircuitBreaker\Policy\RetryPolicyInterface;
use Ksaveras\CircuitBreaker\Storage\StorageInterface;
use Psr\Http\Message\ResponseInterface;
final class CircuitBreaker implements CircuitBreakerInterface
{
public function __construct(
private readonly string $name,
private readonly int $failureThreshold,
private readonly RetryPolicyInterface $retryPolicy,
private readonly StorageInterface $storage,
private readonly HttpHeaderPolicy $headerPolicy = new PolicyChain([]),
) {
}
public function getName(): string
{
return $this->name;
}
public function state(): State
{
return $this->getState($this->getCircuit());
}
public function remainingDelay(): float
{
$circuit = $this->getCircuit();
return match ($this->getState($circuit)) {
State::CLOSED => 0.0,
default => $circuit->getExpirationTime() - microtime(true),
};
}
public function getFailureCount(): int
{
return $this->getCircuit()->getFailureCount();
}
public function isAvailable(): bool
{
return match ($this->state()) {
State::CLOSED,
State::HALF_OPEN => true,
default => false,
};
}
public function isClosed(): bool
{
return State::CLOSED === $this->state();
}
public function isHalfOpen(): bool
{
return State::HALF_OPEN === $this->state();
}
public function isOpen(): bool
{
return State::OPEN === $this->state();
}
/**
* @throws \Throwable
*/
public function call(callable $closure): mixed
{
$circuit = $this->getCircuit();
switch ($this->getState($circuit)) {
case State::CLOSED:
case State::HALF_OPEN:
try {
$result = $closure();
if (0 !== $circuit->getFailureCount()) {
$this->recordSuccess();
}
return $result;
} catch (\Throwable $throwable) {
$this->recordFailure();
throw $throwable;
}
default:
throw new OpenCircuitException();
}
}
public function recordSuccess(): void
{
$this->storage->delete($this->name);
}
public function recordFailure(): void
{
$circuit = $this->getCircuit();
$circuit->increaseFailure($this->retryPolicy);
$this->storage->save($circuit);
}
public function recordRequestFailure(ResponseInterface $response): void
{
if (null === $resetDateTime = $this->headerPolicy->fromResponse($response)) {
$this->recordFailure();
return;
}
$circuit = new Circuit(
$this->name,
1,
$resetDateTime->getTimestamp() - time(),
1,
microtime(true),
);
$this->storage->save($circuit);
}
private function getState(Circuit $circuit): State
{
if ($circuit->thresholdReached()) {
return State::CLOSED;
}
return (microtime(true) - ($circuit->getLastFailure() ?? 0.0)) > $circuit->getResetTimeout() ? State::HALF_OPEN : State::OPEN;
}
private function getCircuit(): Circuit
{
if (null === $circuit = $this->storage->fetch($this->name)) {
return new Circuit($this->name, $this->failureThreshold, $this->retryPolicy->initialDelay());
}
return $circuit;
}
}