|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\TokenBucket; |
|
3
|
|
|
|
|
4
|
|
|
use Psr\Log\LoggerInterface; |
|
5
|
|
|
use RazonYang\TokenBucket\Serializer\PhpSerializer; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Manager is an abstract bucket manager that implements ManagerInterface. |
|
9
|
|
|
*/ |
|
10
|
|
|
abstract class Manager implements ManagerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var LoggerInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $logger; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var int $capacity |
|
19
|
|
|
*/ |
|
20
|
|
|
private $capacity; |
|
21
|
|
|
|
|
22
|
4 |
|
public function getCapacity(): int |
|
23
|
|
|
{ |
|
24
|
4 |
|
return $this->capacity; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var float $rate |
|
29
|
|
|
*/ |
|
30
|
|
|
private $rate; |
|
31
|
|
|
|
|
32
|
4 |
|
public function getRate(): float |
|
33
|
|
|
{ |
|
34
|
4 |
|
return $this->rate; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var SerializerInterface $serializer |
|
39
|
|
|
*/ |
|
40
|
|
|
private $serializer; |
|
41
|
|
|
|
|
42
|
12 |
|
public function __construct(int $capacity, float $rate, LoggerInterface $logger, ?SerializerInterface $serializer = null) |
|
43
|
|
|
{ |
|
44
|
12 |
|
$this->capacity = $capacity; |
|
45
|
12 |
|
$this->rate = $rate; |
|
46
|
12 |
|
$this->logger = $logger; |
|
47
|
12 |
|
$this->serializer = $serializer ?? new PhpSerializer(); |
|
48
|
12 |
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function laodAllowance(string $name): array |
|
51
|
|
|
{ |
|
52
|
1 |
|
$value = $this->load($name); |
|
53
|
1 |
|
if ($value === false) { |
|
54
|
1 |
|
return [$this->capacity, 0]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
try { |
|
58
|
1 |
|
$data = $this->serializer->unserialize($value); |
|
59
|
1 |
|
if (!is_array($data) || count($data) !== 2) { |
|
60
|
|
|
throw new \Exception('invalid data'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
return $data; |
|
64
|
|
|
} catch (\Throwable $e) { |
|
65
|
|
|
$this->logger->error('{name}: {exception}', ['name' => $name, 'exception' => $e->__toString()]); |
|
66
|
|
|
return [$this->capacity, 0]; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Loads allowance, return data if exists, otherwise false. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $name |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed|false |
|
76
|
|
|
*/ |
|
77
|
|
|
abstract protected function load(string $name); |
|
78
|
|
|
|
|
79
|
1 |
|
public function saveAllowance(string $name, int $allowance, int $timestamp) |
|
80
|
|
|
{ |
|
81
|
1 |
|
$value = $this->serializer->serialize([$allowance, $timestamp]); |
|
82
|
1 |
|
$this->save($name, $value); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Saves allowance. |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $name |
|
89
|
|
|
* @param mixed $value |
|
90
|
|
|
* |
|
91
|
|
|
* @throws \Throwable throws an exception if save fails. |
|
92
|
|
|
*/ |
|
93
|
|
|
abstract protected function save(string $name, $value); |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Consumes a token from the bucket. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $name bucket name. |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool returns true on success, otherwise false. |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function consume(string $name, ?int &$remaining = null, ?int &$reset = null): bool |
|
103
|
|
|
{ |
|
104
|
1 |
|
list($allowance, $timestamp) = $this->laodAllowance($name); |
|
105
|
1 |
|
$now = time(); |
|
106
|
1 |
|
$allowance += intval(($now - $timestamp) / $this->rate); |
|
107
|
1 |
|
if ($allowance > $this->capacity) { |
|
108
|
1 |
|
$allowance = $this->capacity; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
if ($allowance < 1) { |
|
112
|
1 |
|
$remaining = 0; |
|
113
|
1 |
|
$reset = intval($this->capacity * $this->rate) - ($now - $timestamp); |
|
114
|
1 |
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
1 |
|
$remaining = $allowance - 1; |
|
118
|
1 |
|
$reset = intval(($this->capacity - $allowance) * $this->rate); |
|
119
|
1 |
|
$this->saveAllowance($name, $remaining, $now); |
|
120
|
1 |
|
return true; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
3 |
|
public function getLimit(int $period): int |
|
124
|
|
|
{ |
|
125
|
3 |
|
return min($this->capacity, intval($period / $this->rate)); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|