|
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
|
|
|
final class LeakyBucketThrottler extends AbstractWindowThrottler implements ThrottlerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @inheritdoc |
|
32
|
|
|
*/ |
|
33
|
|
|
public function hit(): ThrottlerInterface |
|
34
|
|
|
{ |
|
35
|
|
|
$tokenCount = $this->count(); |
|
36
|
|
|
|
|
37
|
|
|
$this->setUsedCapacity($tokenCount + 1); |
|
38
|
|
|
|
|
39
|
|
|
if (0 < $wait = $this->getWaitTime($tokenCount)) { |
|
40
|
|
|
$this->timeProvider->usleep(self::MILLISECOND_TO_MICROSECOND_MULTIPLIER * $wait); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $wait; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function count() |
|
50
|
|
|
{ |
|
51
|
|
|
try { |
|
52
|
|
|
$cachedTime = $this->cache->get($this->getTimeCacheKey()); |
|
|
|
|
|
|
53
|
|
|
$timeSinceLastRequest = self::SECOND_TO_MILLISECOND_MULTIPLIER * ($this->timeProvider->now() - $cachedTime); |
|
54
|
|
|
|
|
55
|
|
|
if ($timeSinceLastRequest > $this->timeLimit) { |
|
|
|
|
|
|
56
|
|
|
return 0; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$lastTokenCount = $this->cache->get($this->getTokenCacheKey()); |
|
60
|
|
|
} catch (ItemNotFoundException $exception) { |
|
|
|
|
|
|
61
|
|
|
$this->clear(); //Clear the bucket |
|
62
|
|
|
|
|
63
|
|
|
return 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Return the `used` token count, minus the amount of tokens which have been `refilled` since the previous request |
|
67
|
|
|
return (int) max(0, ceil($lastTokenCount - ($this->tokenlimit * $timeSinceLastRequest / ($this->timeLimit)))); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
|
|
public function check() |
|
74
|
|
|
{ |
|
75
|
|
|
return 0 === $this->getWaitTime($this->count()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @inheritdoc |
|
80
|
|
|
*/ |
|
81
|
16 |
|
public function getTime() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->timeLimit; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @inheritdoc |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getLimit() |
|
90
|
16 |
|
{ |
|
91
|
16 |
|
return $this->tokenlimit; |
|
92
|
16 |
|
} |
|
93
|
16 |
|
|
|
94
|
16 |
|
/** |
|
95
|
16 |
|
* @inheritdoc |
|
96
|
16 |
|
*/ |
|
97
|
16 |
View Code Duplication |
public function getRetryTimeout() |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
if ($this->threshold > $this->count() + 1) { |
|
|
|
|
|
|
100
|
|
|
return 0; |
|
101
|
|
|
} |
|
102
|
3 |
|
|
|
103
|
|
|
return (int) ceil($this->timeLimit / $this->tokenlimit); |
|
104
|
3 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param int $tokenCount |
|
108
|
|
|
* |
|
109
|
|
|
* @return int |
|
110
|
8 |
|
*/ |
|
111
|
|
View Code Duplication |
private function getWaitTime($tokenCount) |
|
|
|
|
|
|
112
|
8 |
|
{ |
|
113
|
|
|
if ($this->threshold > $tokenCount) { |
|
114
|
8 |
|
return 0; |
|
115
|
|
|
} |
|
116
|
8 |
|
|
|
117
|
2 |
|
return (int) ceil($this->timeLimit / max(1, ($this->tokenlimit - $this->threshold))); |
|
118
|
2 |
|
} |
|
119
|
|
|
|
|
120
|
8 |
|
/** |
|
121
|
|
|
* @param int $tokens |
|
122
|
|
|
*/ |
|
123
|
|
|
private function setUsedCapacity($tokens) |
|
124
|
|
|
{ |
|
125
|
|
|
$this->cache->set($this->getTokenCacheKey(), $tokens, $this->cacheTtl); |
|
|
|
|
|
|
126
|
7 |
|
$this->cache->set($this->getTimeCacheKey(), $this->timeProvider->now(), $this->cacheTtl); |
|
127
|
|
|
} |
|
128
|
7 |
|
|
|
129
|
7 |
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
private function getTokenCacheKey() |
|
133
|
|
|
{ |
|
134
|
14 |
|
return $this->key.self::CACHE_KEY_TOKEN; |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
14 |
|
/** |
|
138
|
13 |
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
13 |
|
private function getTimeCacheKey() |
|
141
|
4 |
|
{ |
|
142
|
|
|
return $this->key.self::CACHE_KEY_TIME; |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: