1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 Krishnaprasad MG <[email protected]> |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Sunspikes\Ratelimit\Throttle\Throttler; |
27
|
|
|
|
28
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException; |
29
|
|
|
use Sunspikes\Ratelimit\Cache\Adapter\CacheAdapterInterface; |
30
|
|
|
use Sunspikes\Ratelimit\Time\TimeAdapterInterface; |
31
|
|
|
|
32
|
|
|
final class LeakyBucketThrottler implements RetriableThrottlerInterface |
33
|
|
|
{ |
34
|
|
|
const TIME_CACHE_KEY = ':time'; |
35
|
|
|
const TOKEN_CACHE_KEY = ':tokens'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var CacheAdapterInterface |
39
|
|
|
*/ |
40
|
|
|
private $cache; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int|null |
44
|
|
|
*/ |
45
|
|
|
private $cacheTtl; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $key; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var int |
54
|
|
|
*/ |
55
|
|
|
private $threshold; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var TimeAdapterInterface |
59
|
|
|
*/ |
60
|
|
|
private $timeProvider; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var int |
64
|
|
|
*/ |
65
|
|
|
private $timeLimit; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
private $tokenlimit; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param CacheAdapterInterface $cache |
74
|
|
|
* @param TimeAdapterInterface $timeAdapter |
75
|
|
|
* @param string $key Cache key prefix |
76
|
|
|
* @param int $tokenLimit Bucket capacity |
77
|
|
|
* @param int $timeLimit Refill time in milliseconds |
78
|
|
|
* @param int|null $threshold Capacity threshold on which to start throttling (default: 0) |
79
|
|
|
* @param int|null $cacheTtl Cache ttl time (default: null => CacheAdapter ttl) |
80
|
|
|
*/ |
81
|
16 |
|
public function __construct( |
82
|
|
|
CacheAdapterInterface $cache, |
83
|
|
|
TimeAdapterInterface $timeAdapter, |
84
|
|
|
$key, |
85
|
|
|
$tokenLimit, |
86
|
|
|
$timeLimit, |
87
|
|
|
$threshold = null, |
88
|
|
|
$cacheTtl = null |
89
|
|
|
) { |
90
|
16 |
|
$this->cache = $cache; |
91
|
16 |
|
$this->timeProvider = $timeAdapter; |
92
|
16 |
|
$this->key = $key; |
93
|
16 |
|
$this->tokenlimit = $tokenLimit; |
94
|
16 |
|
$this->timeLimit = $timeLimit; |
95
|
16 |
|
$this->cacheTtl = $cacheTtl; |
96
|
16 |
|
$this->threshold = null !== $threshold ? $threshold : 0; |
97
|
16 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritdoc |
101
|
|
|
*/ |
102
|
3 |
|
public function access() |
103
|
|
|
{ |
104
|
3 |
|
return 0 === $this->hit(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritdoc |
109
|
|
|
*/ |
110
|
8 |
|
public function hit() |
111
|
|
|
{ |
112
|
8 |
|
$tokenCount = $this->count(); |
113
|
|
|
|
114
|
8 |
|
$this->setUsedCapacity($tokenCount + 1); |
115
|
|
|
|
116
|
8 |
|
if (0 < $wait = $this->getWaitTime($tokenCount)) { |
117
|
2 |
|
$this->timeProvider->usleep(1e3 * $wait); |
118
|
2 |
|
} |
119
|
|
|
|
120
|
8 |
|
return $wait; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
7 |
|
public function clear() |
127
|
|
|
{ |
128
|
7 |
|
$this->setUsedCapacity(0); |
129
|
7 |
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
14 |
|
public function count() |
135
|
|
|
{ |
136
|
|
|
try { |
137
|
14 |
|
$timeSinceLastRequest = 1e3 * ($this->timeProvider->now() - $this->cache->get($this->key.self::TIME_CACHE_KEY)); |
138
|
|
|
|
139
|
13 |
|
if ($timeSinceLastRequest > $this->timeLimit) { |
140
|
4 |
|
return 0; |
141
|
|
|
} |
142
|
|
|
|
143
|
9 |
|
$lastTokenCount = $this->cache->get($this->key.self::TOKEN_CACHE_KEY); |
144
|
10 |
|
} catch (ItemNotFoundException $exception) { |
145
|
6 |
|
$this->clear(); //Clear the bucket |
146
|
|
|
|
147
|
6 |
|
return 0; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Return the `used` token count, minus the amount of tokens which have been `refilled` since the previous request |
151
|
9 |
|
return (int) max(0, ceil($lastTokenCount - ($this->tokenlimit * $timeSinceLastRequest / ($this->timeLimit)))); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
3 |
|
public function check() |
158
|
|
|
{ |
159
|
3 |
|
return 0 === $this->getWaitTime($this->count()); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @inheritdoc |
164
|
|
|
*/ |
165
|
|
|
public function getTime() |
166
|
|
|
{ |
167
|
|
|
return $this->timeLimit; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @inheritdoc |
172
|
|
|
*/ |
173
|
|
|
public function getLimit() |
174
|
|
|
{ |
175
|
|
|
return $this->tokenlimit; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @inheritdoc |
180
|
|
|
*/ |
181
|
2 |
View Code Duplication |
public function getRetryTimeout() |
|
|
|
|
182
|
|
|
{ |
183
|
2 |
|
if ($this->threshold > $this->count() + 1) { |
184
|
1 |
|
return 0; |
185
|
|
|
} |
186
|
|
|
|
187
|
1 |
|
return (int) ceil($this->timeLimit / $this->tokenlimit); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @param int $tokenCount |
192
|
|
|
* |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
9 |
View Code Duplication |
private function getWaitTime($tokenCount) |
|
|
|
|
196
|
|
|
{ |
197
|
9 |
|
if ($this->threshold > $tokenCount) { |
198
|
8 |
|
return 0; |
199
|
|
|
} |
200
|
|
|
|
201
|
3 |
|
return (int) ceil($this->timeLimit / max(1, ($this->tokenlimit - $this->threshold))); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param int $tokens |
206
|
|
|
*/ |
207
|
10 |
|
private function setUsedCapacity($tokens) |
208
|
|
|
{ |
209
|
10 |
|
$this->cache->set($this->key.self::TOKEN_CACHE_KEY, $tokens, $this->cacheTtl); |
210
|
10 |
|
$this->cache->set($this->key.self::TIME_CACHE_KEY, $this->timeProvider->now(), $this->cacheTtl); |
211
|
10 |
|
} |
212
|
|
|
} |
213
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.