|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace NiR\CircuitBreaker\Service; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Defines the configuration of a specific service (max. # failures allowed, opening delay, ...). |
|
9
|
|
|
* |
|
10
|
|
|
* @author Albin Kerouanton <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @internal |
|
13
|
|
|
*/ |
|
14
|
|
|
/* final */ class Configuration |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var string Name of the related service. */ |
|
17
|
|
|
private $serviceName; |
|
18
|
|
|
|
|
19
|
|
|
/** @var int Number of failures allowed before circuit becomes open. */ |
|
20
|
|
|
private $openingThreshold; |
|
21
|
|
|
|
|
22
|
|
|
/** @var int Number of seconds before open circuit become half open. */ |
|
23
|
|
|
private $closingDelay; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int Number of seconds before non-circuit-breaking errors are dismissed. */ |
|
26
|
|
|
private $dismissDelay; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param string $serviceName Name of the related service. |
|
30
|
|
|
* @param int $openingThreshold Number of failures allowed before circuit becomes open. |
|
31
|
|
|
* @param int $closingDelay Number of seconds before open circuit become half open. |
|
32
|
|
|
* @param int $dismissDelay Number of seconds before non-circuit-breaking errors are dismissed. |
|
33
|
|
|
* There is basically no need to break the circuit if the last error was (long) time ago |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(string $serviceName, int $openingThreshold = 20, int $closingDelay = 60, int $dismissDelay = 30) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->serviceName = $serviceName; |
|
38
|
|
|
$this->openingThreshold = $openingThreshold; |
|
39
|
|
|
$this->closingDelay = $closingDelay; |
|
40
|
|
|
$this->dismissDelay = $dismissDelay; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Get the name of this service. |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getServiceName(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->serviceName; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Get the number of failures allowed before circuit opens up. |
|
55
|
|
|
* |
|
56
|
|
|
* @return int |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getOpeningThreshold(): int |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->openingThreshold; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the number of seconds before open circuit becomes half open. |
|
65
|
|
|
* |
|
66
|
|
|
* @return int |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getClosingDelay(): int |
|
69
|
|
|
{ |
|
70
|
|
|
return $this->closingDelay; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the number of seconds before non-circuit-breaking errors are dismissed. |
|
75
|
|
|
* |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getDismissDelay(): int |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->dismissDelay; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|