-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
executable file
·45 lines (33 loc) · 1.02 KB
/
example.py
File metadata and controls
executable file
·45 lines (33 loc) · 1.02 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
from rate_limit import RateLimit, RateLimitExceeded, Or
import tornadoredis
import tornado.ioloop
import tornado.web
import tornado.gen
redis_conn = tornadoredis.Client()
redis_conn.connect()
rl = RateLimit(redis_conn, namespace="example.com")
class MainHandler(tornado.web.RequestHandler):
def user(self):
"""
should return the current user or None
"""
return "vova"
@rl.limit(Or('100/s', 'user:50/h'))
def get(self):
"""
this method is limited to 100 calls a seconds
or 50 times per hour per user
"""
self.finish("Heya, world")
def write_error(self, status_code, **kwargs):
if issubclass(kwargs['exc_info'][0], RateLimitExceeded):
"""
what to do when on rate limit exceeded
"""
self.finish("rate_limit_exceeded")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()