You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
lua-resty-limit-req - Limit the request processing rate between multiple NGINX instances
3
+
lua-resty-redis-ratelimit - Limit the request processing rate between multiple NGINX instances backed by [Redis](https://redis.io/).
4
+
5
+
# Table of Contents
6
+
7
+
*[Status](#status)
8
+
*[Description](#description)
9
+
*[Synopsis](#synopsis)
10
+
*[Methods](#methods)
11
+
*[new](#new)
12
+
*[incoming](#incoming)
13
+
*[set_burst](#set_burst)
14
+
*[Author](#author)
15
+
*[Copyright and License](#copyright-and-license)
16
+
*[See Also](#see-also)
4
17
5
18
# Status
6
19
7
20
Ready for testing. Probably production ready in most cases, though not yet proven in the wild. Please check the issues list and let me know if you have any problems / questions.
8
21
9
-
##Description
22
+
# Description
10
23
11
24
This lua library is a request processing rate limit module for ngx_lua:
12
25
13
26
http://wiki.nginx.org/HttpLuaModule
14
27
15
28
It is used to limit the request processing rate per a defined key between multiple NGINX instances. The limitation is done using the "[leaky bucket](http://en.wikipedia.org/wiki/Leaky_bucket)" method.
16
29
17
-
This module use redis (>= [2.6.0](http://redis.io/commands/eval)) as the backend storage, so you also need the [lua-resty-redis](https://github.com/openresty/lua-resty-redis) library work with it.
30
+

31
+
32
+
This module use Redis (>= [2.6.0](http://redis.io/commands/eval)) as the backend storage, so you also need the [lua-resty-redis](https://github.com/openresty/lua-resty-redis) library work with it.
33
+
34
+
**NOTICE:** If you do not use the `duration` feature and the incoming traffic is **evenly distrbuted**, it is recommended that use the module [resty.limit.req](https://github.com/openresty/lua-resty-limit-traffic/blob/master/lib/resty/limit/req.md) to avoid unnecessary network delays.
Instantiates an object of this class. The class value is returned by the call require `resty.redis.ratelimit`.
50
105
51
-
`syntax: ok = req.limit(cfg)`
106
+
This method takes the following arguments:
52
107
53
-
Available limit configurations are listed as follows:
108
+
*`zone`: Sets the namespace, in particular, we use `<zone>:<key>` string as a unique state identifier inside Redis.
109
+
*`rate`: The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.
110
+
*`burst`: Defines how many requests can make in excess of the rate specified by the zone, default 0.
111
+
*`duration`: The time delay (in seconds) before back to normal state, during this period, the request is always `rejected`, default 0.
54
112
55
-
*`key`
113
+
On failure, this method returns nil and a string describing the error.
56
114
57
-
The key is any non-empty value of the specified variable.
115
+
[Back to TOC](#table-of-contents)
58
116
59
-
*`zone`
117
+
## incoming
60
118
61
-
Sets the namespace, in particular, we use `<zone>:<key>` string as a unique state identifier inside redis.
Fires a new request incoming event and calculates the delay needed (if any) for the current request upon the specified key or whether the user should reject it immediately.
64
122
65
-
The rate is specified in requests per second (r/s). If a rate of less than one request per second is desired, it is specified in request per minute (r/m). For example, half-request per second is 30r/m.
123
+
This method accepts the following arguments:
66
124
67
-
*`interval`
125
+
*`key`: The key is any non-empty value of the specified variable.
126
+
*`redis`: Sets the Redis configuration, `host`, `port`, `timeout` and so on (see below); Instead of the specific Redis configuration, you can also sets the connected Redis `object` directly.
68
127
69
-
The time delay (in s) before back to normal state, default 0.
128
+
```
129
+
- redis.host: Default 127.0.0.1.
130
+
- redis.port: Default 80.
131
+
- redis.timeout: Default 1s.
132
+
- redis.pass: Request for authentication in a password-protected Redis server.
133
+
- redis.dbid: Select the Redis logical database.
134
+
```
70
135
71
-
*`log_level`
136
+
The return values depend on the following cases:
72
137
73
-
Sets the desired logging level for cases when the server refuses to process requests due to rate exceeding, default `ngx.NOTICE`.
138
+
1. If the request does not exceed the `rate` value specified in the [new](#new) method, then this method returns `0` as the delay and the (zero) number of excessive requests per second at the current time.
139
+
2. If the request exceeds the `rate` limit specified in the [new](#new) method but not the `rate` + `burst` value, then this method returns a proper delay (in seconds) for the current request so that it still conform to the `rate` threshold as if it came a bit later rather than now. The 2nd return value indicating the number of excessive reqeusts per second at this point (including the current request).
140
+
3. If the request exceeds the `rate` + `burst` limit, then this method returns `nil` and the error string `"rejected"`.
141
+
4. If an error occurred, then this method returns `nil` and a string describing the error. Such as `"failed to create redis - connection refused"`.
74
142
75
-
*`rds`
143
+
This method never sleeps itself. It simply returns a delay if necessary and requires the caller to later invoke the [ngx.sleep](https://github.com/openresty/lua-nginx-module#ngxsleep) method to sleep.
76
144
77
-
Sets the redis `host` and `port`, default `127.0.0.1` and 6379.
145
+
[Back to TOC](#table-of-contents)
78
146
79
-
- rds.pass: auth redis.
80
-
- rds.dbid: select database.
147
+
## set_burst
81
148
82
-
*`conn`
149
+
**syntax:**`obj:set_burst(burst)`
83
150
84
-
Instead of the specific redis configuration (aka. `rds`), sets the connected redis object directly.
151
+
Overwrites the `burst` threshold as specified in the [new](#new) method.
85
152
153
+
[Back to TOC](#table-of-contents)
86
154
87
155
# Author
88
156
89
157
Monkey Zhang <timebug.info@gmail.com>, UPYUN Inc.
90
158
91
159
Inspired from http://nginx.org/en/docs/http/ngx_http_limit_req_module.html.
92
160
93
-
# Licence
161
+
[Back to TOC](#table-of-contents)
162
+
163
+
# Copyright and License
94
164
95
165
This module is licensed under the 2-clause BSD license.
96
166
@@ -105,3 +175,13 @@ Redistribution and use in source and binary forms, with or without modification,
105
175
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
106
176
107
177
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178
+
179
+
[Back to TOC](#table-of-contents)
180
+
181
+
# See Also
182
+
183
+
* Rate Limiting with NGINX: https://www.nginx.com/blog/rate-limiting-nginx/
184
+
* the ngx_lua module: https://github.com/openresty/lua-nginx-module
0 commit comments