-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathMemcachedRateLimiter.php
113 lines (90 loc) Β· 3.32 KB
/
MemcachedRateLimiter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace RateLimit;
use Memcached;
use RateLimit\Exception\CannotUseRateLimiter;
use RateLimit\Exception\LimitExceeded;
use function max;
use function sprintf;
use function time;
final class MemcachedRateLimiter extends ConfigurableRateLimiter implements RateLimiter, SilentRateLimiter
{
private const MEMCACHED_SECONDS_LIMIT = 2592000; // 30 days in seconds
private Memcached $memcached;
private string $keyPrefix;
public function __construct(Rate $rate, Memcached $memcached, string $keyPrefix = '')
{
// @see https://www.php.net/manual/en/memcached.increment.php#111187
if ($memcached->getOption(Memcached::OPT_BINARY_PROTOCOL) !== 1) {
throw new CannotUseRateLimiter('Memcached "OPT_BINARY_PROTOCOL" option should be set to "true".');
}
parent::__construct($rate);
$this->memcached = $memcached;
$this->keyPrefix = $keyPrefix;
}
public function limit(string $identifier): void
{
$limitKey = $this->limitKey($identifier);
$current = $this->getCurrent($limitKey);
if ($current >= $this->rate->getOperations()) {
throw LimitExceeded::for($identifier, $this->rate);
}
$this->updateCounter($limitKey);
}
public function limitSilently(string $identifier): Status
{
$limitKey = $this->limitKey($identifier);
$timeKey = $this->timeKey($identifier);
$current = $this->getCurrent($limitKey);
if ($current <= $this->rate->getOperations()) {
$current = $this->updateCounterAndTime($limitKey, $timeKey);
}
return Status::from(
$identifier,
$current,
$this->rate->getOperations(),
time() + max(0, $this->rate->getInterval() - $this->getElapsedTime($timeKey))
);
}
private function limitKey(string $identifier): string
{
return sprintf('%s%s:%d', $this->keyPrefix, $identifier, $this->rate->getInterval());
}
private function timeKey(string $identifier): string
{
return sprintf('%s%s:%d:time', $this->keyPrefix, $identifier, $this->rate->getInterval());
}
private function getCurrent(string $limitKey): int
{
return (int) $this->memcached->get($limitKey);
}
private function updateCounterAndTime(string $limitKey, string $timeKey): int
{
$current = $this->updateCounter($limitKey);
if ($current === 1) {
$this->memcached->add($timeKey, time(), $this->intervalToMemcachedTime($this->rate->getInterval()));
}
return $current;
}
private function updateCounter(string $limitKey): int
{
$current = $this->memcached->increment($limitKey, 1, 1, $this->intervalToMemcachedTime($this->rate->getInterval()));
return $current === false ? 1 : $current;
}
private function getElapsedTime(string $timeKey): int
{
return time() - (int) $this->memcached->get($timeKey);
}
/**
* Interval to Memcached expiration time.
*
* @see https://www.php.net/manual/en/memcached.expiration.php
*
* @param int $interval
* @return int
*/
private function intervalToMemcachedTime(int $interval): int
{
return $interval <= self::MEMCACHED_SECONDS_LIMIT ? $interval : time() + $interval;
}
}