-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Expand file tree
/
Copy pathLimit.php
More file actions
195 lines (171 loc) · 4.06 KB
/
Limit.php
File metadata and controls
195 lines (171 loc) · 4.06 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace Illuminate\Cache\RateLimiting;
class Limit
{
/**
* The rate limit signature key.
*
* @var mixed
*/
public $key;
/**
* The maximum number of attempts allowed within the given number of seconds.
*
* @var int
*/
public $maxAttempts;
/**
* The number of seconds until the rate limit is reset.
*
* @var int
*/
public $decaySeconds;
/**
* Indicates if the sliding window algorithm should be used.
*
* @var bool
*/
public $slidingWindow = false;
/**
* The after callback used to determine if the limiter should be hit.
*
* @var ?callable
*/
public $afterCallback = null;
/**
* The response generator callback.
*
* @var callable
*/
public $responseCallback;
/**
* Create a new limit instance.
*
* @param mixed $key
* @param int $maxAttempts
* @param int $decaySeconds
*/
public function __construct($key = '', int $maxAttempts = 60, int $decaySeconds = 60)
{
$this->key = $key;
$this->maxAttempts = $maxAttempts;
$this->decaySeconds = $decaySeconds;
}
/**
* Create a new rate limit.
*
* @param int $maxAttempts
* @param int $decaySeconds
* @return static
*/
public static function perSecond($maxAttempts, $decaySeconds = 1)
{
return new static('', $maxAttempts, $decaySeconds);
}
/**
* Create a new rate limit.
*
* @param int $maxAttempts
* @param int $decayMinutes
* @return static
*/
public static function perMinute($maxAttempts, $decayMinutes = 1)
{
return new static('', $maxAttempts, 60 * $decayMinutes);
}
/**
* Create a new rate limit using minutes as decay time.
*
* @param int $decayMinutes
* @param int $maxAttempts
* @return static
*/
public static function perMinutes($decayMinutes, $maxAttempts)
{
return new static('', $maxAttempts, 60 * $decayMinutes);
}
/**
* Create a new rate limit using hours as decay time.
*
* @param int $maxAttempts
* @param int $decayHours
* @return static
*/
public static function perHour($maxAttempts, $decayHours = 1)
{
return new static('', $maxAttempts, 60 * 60 * $decayHours);
}
/**
* Create a new rate limit using days as decay time.
*
* @param int $maxAttempts
* @param int $decayDays
* @return static
*/
public static function perDay($maxAttempts, $decayDays = 1)
{
return new static('', $maxAttempts, 60 * 60 * 24 * $decayDays);
}
/**
* Create a new unlimited rate limit.
*
* @return static
*/
public static function none()
{
return new Unlimited;
}
/**
* Set the key of the rate limit.
*
* @param mixed $key
* @return $this
*/
public function by($key)
{
$this->key = $key;
return $this;
}
/**
* Set the rate limiter to use the sliding window algorithm.
*
* @return $this
*/
public function slidingWindow()
{
$this->slidingWindow = true;
return $this;
}
/**
* Set the callback to determine if the limiter should be hit.
*
* @param callable $callback
* @return $this
*/
public function after($callback)
{
$this->afterCallback = $callback;
return $this;
}
/**
* Set the callback that should generate the response when the limit is exceeded.
*
* @param callable $callback
* @return $this
*/
public function response(callable $callback)
{
$this->responseCallback = $callback;
return $this;
}
/**
* Get a potential fallback key for the limit.
*
* @return string
*/
public function fallbackKey()
{
$prefix = $this->key ? "{$this->key}:" : '';
return "{$prefix}attempts:{$this->maxAttempts}:decay:{$this->decaySeconds}";
}
}