1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the bugloos/fault-tolerance-bundle project. |
5
|
|
|
* (c) Bugloos <https://bugloos.com/> |
6
|
|
|
* For the full copyright and license information, please view |
7
|
|
|
* the LICENSE file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Bugloos\FaultToleranceBundle\Config; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Mojtaba Gheytasi <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Config |
16
|
|
|
{ |
17
|
|
|
private array $config = [ |
18
|
|
|
'circuitBreaker' => [ |
19
|
|
|
'enabled' => true, |
20
|
|
|
'forceOpen' => false, |
21
|
|
|
'forceClosed' => false, |
22
|
|
|
'timeWindow' => 50, |
23
|
|
|
'failureRateThreshold' => 10, |
24
|
|
|
'intervalToHalfOpen' => 30, |
25
|
|
|
], |
26
|
|
|
'fallback' => [ |
27
|
|
|
'enabled' => true, |
28
|
|
|
], |
29
|
|
|
'requestCache' => [ |
30
|
|
|
'enabled' => false, |
31
|
|
|
'expiresCacheAfter' => 30,//seconds |
32
|
|
|
'storage' => 'redis', |
33
|
|
|
], |
34
|
|
|
'requestLog' => [ |
35
|
|
|
'enabled' => false, |
36
|
|
|
], |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
public function toArray(): array |
40
|
|
|
{ |
41
|
|
|
return $this->config; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function enableFallback(): self |
45
|
|
|
{ |
46
|
|
|
$this->config['fallback']['enabled'] = true; |
47
|
|
|
|
48
|
|
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function disableFallback(): self |
52
|
|
|
{ |
53
|
|
|
$this->config['fallback']['enabled'] = false; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function enableCache(): self |
59
|
|
|
{ |
60
|
|
|
$this->config['requestCache']['enabled'] = true; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function disableCache(): self |
66
|
|
|
{ |
67
|
|
|
$this->config['requestCache']['enabled'] = false; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function cacheStorage(string $storage): self |
73
|
|
|
{ |
74
|
|
|
$this->config['requestCache']['storage'] = $storage; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function expiresCacheAfter(int $second): self |
80
|
|
|
{ |
81
|
|
|
$this->config['requestCache']['expiresCacheAfter'] = $second; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function enableLog(): self |
87
|
|
|
{ |
88
|
|
|
$this->config['requestLog']['enabled'] = true; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function disableLog(): self |
94
|
|
|
{ |
95
|
|
|
$this->config['requestLog']['enabled'] = false; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function enableCircuitBreaker(): self |
101
|
|
|
{ |
102
|
|
|
$this->config['circuitBreaker']['enabled'] = true; |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function disableCircuitBreaker(): self |
108
|
|
|
{ |
109
|
|
|
$this->config['circuitBreaker']['enabled'] = false; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function forceOpenCircuitBreaker(bool $forceOpen = true): self |
115
|
|
|
{ |
116
|
|
|
$this->config['circuitBreaker']['forceOpen'] = $forceOpen; |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function forceCloseCircuitBreaker(bool $forceClose = true): self |
122
|
|
|
{ |
123
|
|
|
$this->config['circuitBreaker']['forceClosed'] = $forceClose; |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function timeWindow(int $timeWindow): self |
129
|
|
|
{ |
130
|
|
|
$this->config['circuitBreaker']['timeWindow'] = $timeWindow; |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function failureRateThreshold(int $failureRate): self |
136
|
|
|
{ |
137
|
|
|
$this->config['circuitBreaker']['failureRateThreshold'] = $failureRate; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function intervalToHalfOpen(int $halfOpenTime): self |
143
|
|
|
{ |
144
|
|
|
$this->config['circuitBreaker']['intervalToHalfOpen'] = $halfOpenTime; |
145
|
|
|
|
146
|
|
|
return $this; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|