diff --git a/zerver/lib/rate_limiter.py b/zerver/lib/rate_limiter.py index 71634f64f6..86e8b81bd9 100644 --- a/zerver/lib/rate_limiter.py +++ b/zerver/lib/rate_limiter.py @@ -1,6 +1,7 @@ from __future__ import absolute_import from django.conf import settings +from zerver.lib.redis_utils import get_redis_client import redis import time @@ -11,7 +12,7 @@ from itertools import izip # Implement a rate-limiting scheme inspired by the one described here, but heavily modified # http://blog.domaintools.com/2013/04/rate-limiting-with-redis/ -client = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) +client = get_redis_client() rules = settings.RATE_LIMITING_RULES def _rules_for_user(user): if user.rate_limits != "": diff --git a/zerver/lib/redis_utils.py b/zerver/lib/redis_utils.py new file mode 100644 index 0000000000..7cb951d3bc --- /dev/null +++ b/zerver/lib/redis_utils.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from django.conf import settings + +import redis + +def get_redis_client(): + return redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) diff --git a/zerver/lib/socket.py b/zerver/lib/socket.py index 97a71c9081..83e75eb41d 100644 --- a/zerver/lib/socket.py +++ b/zerver/lib/socket.py @@ -10,7 +10,6 @@ import tornado.ioloop import ujson import logging import time -import redis from zerver.models import UserProfile, get_user_profile_by_id, get_client from zerver.lib.queue import queue_json_publish @@ -20,6 +19,7 @@ from zerver.lib.utils import statsd from zerver.lib.event_queue import get_client_descriptor from zerver.middleware import record_request_start_data, record_request_stop_data, \ record_request_restart_data, write_log_line, format_timedelta +from zerver.lib.redis_utils import get_redis_client logger = logging.getLogger('zulip.socket') @@ -57,7 +57,7 @@ def register_connection(id, conn): def deregister_connection(conn): del connections[conn.client_id] -redis_client = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) +redis_client = get_redis_client() def req_redis_key(req_id): return 'socket_req_status:%s' % (req_id,) diff --git a/zerver/worker/queue_processors.py b/zerver/worker/queue_processors.py index d4524a3e2f..8ebc6f7977 100644 --- a/zerver/worker/queue_processors.py +++ b/zerver/worker/queue_processors.py @@ -22,6 +22,7 @@ from zerver.lib.socket import req_redis_key from confirmation.models import Confirmation from zerver.lib.db import reset_queries from django.core.mail import EmailMessage +from zerver.lib.redis_utils import get_redis_client import os import sys @@ -32,7 +33,6 @@ import time import datetime import logging import simplejson -import redis import StringIO def assign_queue(queue_name, enabled=True): @@ -262,7 +262,7 @@ class SlowQueryWorker(QueueProcessingWorker): class MessageSenderWorker(QueueProcessingWorker): def __init__(self): super(MessageSenderWorker, self).__init__() - self.redis_client = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) + self.redis_client = get_redis_client() self.handler = BaseHandler() self.handler.load_middleware() diff --git a/zilencer/views.py b/zilencer/views.py index 628d6acbb1..323a76cd97 100644 --- a/zilencer/views.py +++ b/zilencer/views.py @@ -7,6 +7,7 @@ from django.template import RequestContext, loader from zerver.decorator import has_request_variables, REQ, json_to_dict from zerver.lib.actions import internal_send_message +from zerver.lib.redis_utils import get_redis_client from zerver.lib.response import json_success, json_error, json_response, json_method_not_allowed from zerver.lib.rest import rest_dispatch as _rest_dispatch from zerver.models import get_realm, get_user_profile_by_email, resolve_email_to_domain, \ @@ -16,11 +17,10 @@ from error_notify import notify_server_error, notify_browser_error from django.core.mail import send_mail from django.conf import settings import time -import redis rest_dispatch = csrf_exempt((lambda request, *args, **kwargs: _rest_dispatch(request, globals(), *args, **kwargs))) -client = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT, db=0) +client = get_redis_client() def has_enough_time_expired_since_last_message(sender_email, min_delay): # This function returns a boolean, but it also has the side effect