1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace LeoCarmo\CircuitBreaker; |
4
|
|
|
|
5
|
|
|
use Psr\Http\Message\RequestInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
class GuzzleMiddleware |
9
|
|
|
{ |
10
|
|
|
protected CircuitBreaker $circuitBreaker; |
11
|
|
|
|
12
|
|
|
protected array $customSuccessCodes = []; |
13
|
|
|
|
14
|
|
|
protected array $customIgnoreCodes = []; |
15
|
|
|
|
16
|
|
|
public function __construct(CircuitBreaker $circuitBreaker) |
17
|
|
|
{ |
18
|
|
|
$this->circuitBreaker = $circuitBreaker; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setCustomSuccessCodes(array $codes): void |
22
|
|
|
{ |
23
|
|
|
$this->customSuccessCodes = $codes; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setCustomIgnoreCodes(array $codes): void |
27
|
|
|
{ |
28
|
|
|
$this->customIgnoreCodes = $codes; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __invoke(callable $handler): \Closure |
32
|
|
|
{ |
33
|
|
|
return function (RequestInterface $request, array $options) use ($handler) { |
34
|
|
|
|
35
|
|
|
if (! $this->circuitBreaker->isAvailable()) { |
36
|
|
|
throw new CircuitBreakerException( |
37
|
|
|
sprintf('"%s" is not available', $this->circuitBreaker->getService()) |
38
|
|
|
); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$promise = $handler($request, $options); |
42
|
|
|
|
43
|
|
|
return $promise->then( |
44
|
|
|
function (ResponseInterface $response) { |
45
|
|
|
$this->executeCircuitBreakerOnResponse($response); |
46
|
|
|
|
47
|
|
|
return $response; |
48
|
|
|
}, |
49
|
|
|
function (\Throwable $exception) { |
50
|
|
|
$this->circuitBreaker->failure(); |
51
|
|
|
throw $exception; |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
}; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function executeCircuitBreakerOnResponse(ResponseInterface $response): void |
58
|
|
|
{ |
59
|
|
|
$statusCode = $response->getStatusCode(); |
60
|
|
|
|
61
|
|
|
if (in_array($statusCode, $this->customIgnoreCodes)) { |
62
|
|
|
return; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (! $this->isStatusCodeRangeValid($statusCode)) { |
66
|
|
|
$this->circuitBreaker->failure(); |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($this->isStatusCodeRedirect($statusCode)) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($this->isStatusCodeSuccess($statusCode) || in_array($statusCode, $this->customSuccessCodes)) { |
75
|
|
|
$this->circuitBreaker->success(); |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$this->circuitBreaker->failure(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
protected function isStatusCodeRangeValid(int $statusCode): bool |
83
|
|
|
{ |
84
|
|
|
return ($statusCode >= 100 && $statusCode < 600); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function isStatusCodeRedirect(int $statusCode): bool |
88
|
|
|
{ |
89
|
|
|
return ($statusCode >= 300 && $statusCode < 400); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function isStatusCodeSuccess(int $statusCode): bool |
93
|
|
|
{ |
94
|
|
|
return ($statusCode >= 200 && $statusCode < 300); |
95
|
|
|
} |
96
|
|
|
} |