Mercurial > public > sg101
comparison gpp/antispam/rate_limit.py @ 508:6f5fff924877
Created a centralized spot to get a Redis connection so that settings can be managed in one place.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 04 Dec 2011 19:53:27 +0000 |
parents | 32cec6cd8808 |
children | 6a265b5768ca |
comparison
equal
deleted
inserted
replaced
507:8631d32e6b16 | 508:6f5fff924877 |
---|---|
6 import logging | 6 import logging |
7 | 7 |
8 import redis | 8 import redis |
9 from django.conf import settings | 9 from django.conf import settings |
10 | 10 |
11 from core.services import get_redis_connection | |
12 | |
11 | 13 |
12 logger = logging.getLogger(__name__) | 14 logger = logging.getLogger(__name__) |
13 | |
14 # Redis connection and database settings | |
15 HOST = getattr(settings, 'RATE_LIMIT_REDIS_HOST', 'localhost') | |
16 PORT = getattr(settings, 'RATE_LIMIT_REDIS_PORT', 6379) | |
17 DB = getattr(settings, 'RATE_LIMIT_REDIS_DB', 0) | |
18 | 15 |
19 | 16 |
20 # This exception is thrown upon any Redis error. This insulates client code from | 17 # This exception is thrown upon any Redis error. This insulates client code from |
21 # knowing that we are using Redis and will allow us to use something else in the | 18 # knowing that we are using Redis and will allow us to use something else in the |
22 # future. | 19 # future. |
35 def _get_connection(): | 32 def _get_connection(): |
36 """ | 33 """ |
37 Create and return a Redis connection. Returns None on failure. | 34 Create and return a Redis connection. Returns None on failure. |
38 """ | 35 """ |
39 try: | 36 try: |
40 conn = redis.Redis(host=HOST, port=PORT, db=DB) | 37 conn = get_redis_connection() |
41 except redis.RedisError, e: | 38 except redis.RedisError, e: |
42 logger.error("rate limit: %s" % e) | 39 logger.error("rate limit: %s" % e) |
43 raise RateLimiterUnavailable | 40 raise RateLimiterUnavailable |
44 | 41 |
45 return conn | 42 return conn |