|
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\Adapter\CacheAdapterInterface; |
|
29
|
|
|
use Sunspikes\Ratelimit\Cache\Exception\ItemNotFoundException; |
|
30
|
|
|
|
|
31
|
|
|
class ElasticWindowThrottler implements RetriableThrottlerInterface, \Countable |
|
32
|
|
|
{ |
|
33
|
|
|
/* @var CacheAdapterInterface */ |
|
34
|
|
|
protected $cache; |
|
35
|
|
|
/* @var string */ |
|
36
|
|
|
protected $key; |
|
37
|
|
|
/* @var int */ |
|
38
|
|
|
protected $limit; |
|
39
|
|
|
/* @var int */ |
|
40
|
|
|
protected $ttl; |
|
41
|
|
|
/* @var int */ |
|
42
|
|
|
protected $counter; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param CacheAdapterInterface $cache |
|
46
|
|
|
* @param string $key |
|
47
|
|
|
* @param int $limit |
|
48
|
|
|
* @param int $ttl |
|
49
|
|
|
*/ |
|
50
|
14 |
|
public function __construct(CacheAdapterInterface $cache, $key, $limit, $ttl) |
|
51
|
|
|
{ |
|
52
|
14 |
|
$this->cache = $cache; |
|
53
|
14 |
|
$this->key = $key; |
|
54
|
14 |
|
$this->limit = $limit; |
|
55
|
14 |
|
$this->ttl = $ttl; |
|
56
|
14 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @inheritdoc |
|
60
|
|
|
*/ |
|
61
|
4 |
|
public function access() |
|
62
|
|
|
{ |
|
63
|
4 |
|
$status = $this->check(); |
|
64
|
|
|
|
|
65
|
4 |
|
$this->hit(); |
|
66
|
|
|
|
|
67
|
4 |
|
return $status; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @inheritdoc |
|
72
|
|
|
*/ |
|
73
|
10 |
|
public function hit() |
|
74
|
|
|
{ |
|
75
|
10 |
|
$this->counter = $this->count() + 1; |
|
76
|
|
|
|
|
77
|
10 |
|
$this->cache->set($this->key, $this->counter, $this->ttl); |
|
78
|
|
|
|
|
79
|
10 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
2 |
|
public function clear() |
|
86
|
|
|
{ |
|
87
|
2 |
|
$this->counter = 0; |
|
88
|
|
|
|
|
89
|
2 |
|
$this->cache->set($this->key, $this->counter, $this->ttl); |
|
90
|
|
|
|
|
91
|
2 |
|
return $this; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @inheritdoc |
|
96
|
|
|
*/ |
|
97
|
12 |
|
public function count() |
|
98
|
|
|
{ |
|
99
|
12 |
|
if (!is_null($this->counter)) { |
|
100
|
11 |
|
return $this->counter; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
try { |
|
104
|
11 |
|
$this->counter = $this->cache->get($this->key); |
|
105
|
11 |
|
} catch (ItemNotFoundException $e) { |
|
106
|
5 |
|
$this->counter = 0; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
11 |
|
return $this->counter; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
8 |
|
public function check() |
|
116
|
|
|
{ |
|
117
|
8 |
|
return ($this->count() < $this->limit); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @inheritdoc |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getTime() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->ttl; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @inheritdoc |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getLimit() |
|
132
|
|
|
{ |
|
133
|
|
|
return $this->limit; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @inheritdoc |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function getRetryTimeout() |
|
140
|
|
|
{ |
|
141
|
1 |
|
if ($this->check()) { |
|
142
|
1 |
|
return 0; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
1 |
|
return 1e3 * $this->ttl; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|