|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ================================== |
|
4
|
|
|
* Responsible PHP API |
|
5
|
|
|
* ================================== |
|
6
|
|
|
* |
|
7
|
|
|
* @link Git https://github.com/vince-scarpa/responsibleAPI.git |
|
8
|
|
|
* |
|
9
|
|
|
* @api Responible API |
|
10
|
|
|
* @package responsible\core\throttle |
|
11
|
|
|
* |
|
12
|
|
|
* @author Vince scarpa <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace responsible\core\throttle; |
|
16
|
|
|
|
|
17
|
|
|
use responsible\core\server; |
|
18
|
|
|
use responsible\core\exception; |
|
19
|
|
|
|
|
20
|
|
|
class limiterOptions |
|
21
|
|
|
{ |
|
22
|
|
|
use \responsible\core\traits\optionsTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* [$capacity Bucket volume] |
|
26
|
|
|
* @var integer |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $capacity = 100; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* [$leakRate Constant rate at which the bucket will leak] |
|
32
|
|
|
* @var string|integer |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $leakRate = 1; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* [$timeframe Durations are in seconds] |
|
38
|
|
|
* @var array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static $timeframe = [ |
|
41
|
|
|
'SECOND' => 1, |
|
42
|
|
|
'MINUTE' => 60, |
|
43
|
|
|
'HOUR' => 3600, |
|
44
|
|
|
'DAY' => 86400, |
|
45
|
|
|
'CUSTOM' => 0, |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* [$window Timeframe window] |
|
50
|
|
|
* @var integer|string |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $window = 'MINUTE'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* [$unlimited Rate limiter bypass if true] |
|
56
|
|
|
* @var boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $unlimited = false; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* [$account User account object] |
|
62
|
|
|
*/ |
|
63
|
|
|
protected $account; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* [$isMockTest Set the mock test] |
|
67
|
|
|
* @var boolean |
|
68
|
|
|
*/ |
|
69
|
|
|
protected $isMockTest = false; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* [$mockAccount Set the mock account] |
|
73
|
|
|
* @var object |
|
74
|
|
|
*/ |
|
75
|
|
|
protected $mockAccount; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* [$scope Set the default scope] |
|
79
|
|
|
* @var string |
|
80
|
|
|
*/ |
|
81
|
|
|
protected $scope = 'private'; |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* [hasOptionProperty Check if an option property is set] |
|
85
|
|
|
* @param array $options |
|
86
|
|
|
* @param string $property |
|
87
|
|
|
* @return string|integer|boolean |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function hasOptionProperty(array $options, $property, $default = false) |
|
90
|
|
|
{ |
|
91
|
|
|
$val = isset($options[$property]) ? $options[$property] : $default; |
|
92
|
|
|
|
|
93
|
|
|
if ($val && empty($options[$property])) { |
|
94
|
|
|
$val = $default; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $val; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* [setCapacity Set the buckets capacity] |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function setCapacity($limit = null) |
|
104
|
|
|
{ |
|
105
|
|
|
$options = $this->getOptions(); |
|
106
|
|
|
|
|
107
|
|
|
if (!is_null($limit)) { |
|
108
|
|
|
$options['rateLimit'] = $limit; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$hasCapacityOption = $this->hasOptionProperty($options, 'rateLimit'); |
|
112
|
|
|
|
|
113
|
|
|
if (!is_numeric($hasCapacityOption) || empty($hasCapacityOption)) { |
|
114
|
|
|
$hasCapacityOption = false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->capacity = ($hasCapacityOption) ? intval($hasCapacityOption) : intval($this->capacity); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* [getCapacity Get the buckets capacity] |
|
122
|
|
|
* @return integer |
|
123
|
|
|
*/ |
|
124
|
|
|
public function getCapacity() |
|
125
|
|
|
{ |
|
126
|
|
|
return $this->capacity; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* [setTimeframe Set the window timeframe] |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function setTimeframe($rate = null) |
|
133
|
|
|
{ |
|
134
|
|
|
$options = $this->getOptions(); |
|
135
|
|
|
|
|
136
|
|
|
if (!is_null($rate)) { |
|
137
|
|
|
$options['rateWindow'] = $rate; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$timeframe = $this->hasOptionProperty($options, 'rateWindow'); |
|
141
|
|
|
|
|
142
|
|
|
if (is_string($timeframe)) { |
|
143
|
|
|
if (isset(self::$timeframe[$timeframe])) { |
|
144
|
|
|
$this->window = intval(self::$timeframe[$timeframe]); |
|
145
|
|
|
return; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if (is_numeric($timeframe)) { |
|
150
|
|
|
if ($timeframe < 0) { |
|
151
|
|
|
$timeframe = ($timeframe*-1); |
|
152
|
|
|
} |
|
153
|
|
|
self::$timeframe['CUSTOM'] = $timeframe; |
|
154
|
|
|
$this->window = intval(self::$timeframe['CUSTOM']); |
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$this->window = self::$timeframe['MINUTE']; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* [getTimeframe Get the timeframe window] |
|
163
|
|
|
* @return integer|string |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getTimeframe() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->window; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* [setLeakRate Set the buckets leak rate] |
|
172
|
|
|
* Options: slow, medium, normal, default, fast or custom positive integer |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function setLeakRate() |
|
175
|
|
|
{ |
|
176
|
|
|
$options = $this->getOptions(); |
|
177
|
|
|
|
|
178
|
|
|
if (isset($options['leak']) && !$options['leak']) { |
|
179
|
|
|
$options['leakRate'] = 'default'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
$leakRate = $this->hasOptionProperty($options, 'leakRate'); |
|
183
|
|
|
|
|
184
|
|
|
if (empty($leakRate) || !is_string($leakRate)) { |
|
185
|
|
|
$leakRate = 'default'; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
$this->leakRate = $leakRate; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* [getLeakRate Get the buckets leak rate] |
|
193
|
|
|
* @return string|integer |
|
194
|
|
|
*/ |
|
195
|
|
|
public function getLeakRate() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->leakRate; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* [setUnlimited Rate limiter bypass] |
|
202
|
|
|
*/ |
|
203
|
|
|
protected function setUnlimited() |
|
204
|
|
|
{ |
|
205
|
|
|
$options = $this->getOptions(); |
|
206
|
|
|
|
|
207
|
|
|
$unlimited = false; |
|
208
|
|
|
|
|
209
|
|
|
if (isset($options['unlimited']) && ($options['unlimited'] == 1 || $options['unlimited'] == true)) { |
|
210
|
|
|
$unlimited = true; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$this->unlimited = $unlimited; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* [setUnlimited Rate limiter bypass in debug mode] |
|
218
|
|
|
*/ |
|
219
|
|
|
protected function setDebugMode() |
|
220
|
|
|
{ |
|
221
|
|
|
$options = $this->getOptions(); |
|
222
|
|
|
|
|
223
|
|
|
$unlimited = false; |
|
224
|
|
|
|
|
225
|
|
|
if (isset($options['requestType']) && $options['requestType'] === 'debug') { |
|
226
|
|
|
$unlimited = true; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
$this->unlimited = $unlimited; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* [isUnlimited Check if the Responsible API is set to unlimited] |
|
234
|
|
|
* @return boolean |
|
235
|
|
|
*/ |
|
236
|
|
|
protected function isUnlimited() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->unlimited; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|