| Conditions | 4 |
| Total Lines | 19 |
| Code Lines | 17 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import time |
||
| 33 | def __init__(self, key_prefix, limit, per): |
||
| 34 | global redis |
||
| 35 | if redis is None and current_app.config['USE_RATE_LIMITS']: |
||
| 36 | if current_app.config['TESTING']: |
||
| 37 | redis = FakeRedis() |
||
| 38 | else: # pragma: no cover |
||
| 39 | redis_host = current_app.config.get("REDIS_HOST", "localhost") |
||
| 40 | redis_port = current_app.config.get("REDIS_PORT", 6379) |
||
| 41 | redis_db = current_app.config.get("REDIS_DB", 0) |
||
| 42 | redis = Redis(host=redis_host, port=redis_port, db=redis_db) |
||
| 43 | |||
| 44 | self.reset = (int(time.time()) // per) * per + per |
||
| 45 | self.key = key_prefix + str(self.reset) |
||
| 46 | self.limit = limit |
||
| 47 | self.per = per |
||
| 48 | p = redis.pipeline() |
||
| 49 | p.incr(self.key) |
||
| 50 | p.expireat(self.key, self.reset + self.expiration_window) |
||
| 51 | self.current = min(p.execute()[0], limit) |
||
| 52 | |||
| 60 |