-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
25 lines (19 loc) · 685 Bytes
/
app.py
File metadata and controls
25 lines (19 loc) · 685 Bytes
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
import redis
from flask import Flask, request
from api_throttler import SlidingWindowThrottler, SlidingWindowThrottlerRedis
app = Flask(__name__)
cache = redis.Redis(host='redis', port=6379)
# throttler = SlidingWindowThrottler(calls=3, period=10)
throttler = SlidingWindowThrottlerRedis(calls=3, period=10, cache=cache)
@app.route('/')
def is_throttled():
if 'user' in request.args:
user = request.args['user']
else:
user = 'nobody'
if not throttler.is_throttled(user):
return f'You are Not throttled, {user}.'
else:
return f'You are throttled, {user}.'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)