Commit 83bdb08e authored by Sean Bleier's avatar Sean Bleier

Add support for providing a socket timeout to redis-py.

parent ff014f34
...@@ -21,7 +21,7 @@ clean: ...@@ -21,7 +21,7 @@ clean:
.PHONY: test .PHONY: test
test: install_requirements test: install_requirements
PYTHONPATH=$(PYTHONPATH): django-admin.py test --settings=tests.settings -s PYTHONPATH=$(PYTHONPATH): django-admin.py test tests.testapp.tests.socket_timeout_tests:SocketTimeoutTestCase.test_socket_timeout --settings=tests.settings -s
.PHONY: shell .PHONY: shell
shell: shell:
......
...@@ -55,6 +55,7 @@ class BaseRedisCache(BaseCache): ...@@ -55,6 +55,7 @@ class BaseRedisCache(BaseCache):
self.password = self.get_password() self.password = self.get_password()
self.parser_class = self.get_parser_class() self.parser_class = self.get_parser_class()
self.pickle_version = self.get_pickle_version() self.pickle_version = self.get_pickle_version()
self.socket_timeout = self.get_socket_timeout()
self.connection_pool_class = self.get_connection_pool_class() self.connection_pool_class = self.get_connection_pool_class()
self.connection_pool_class_kwargs = ( self.connection_pool_class_kwargs = (
self.get_connection_pool_class_kwargs() self.get_connection_pool_class_kwargs()
...@@ -105,6 +106,9 @@ class BaseRedisCache(BaseCache): ...@@ -105,6 +106,9 @@ class BaseRedisCache(BaseCache):
except (ValueError, TypeError): except (ValueError, TypeError):
raise ImproperlyConfigured("pickle version value must be an integer") raise ImproperlyConfigured("pickle version value must be an integer")
def get_socket_timeout(self):
return self.options.get('SOCKET_TIMEOUT', None)
def get_connection_pool_class(self): def get_connection_pool_class(self):
pool_class = self.options.get('CONNECTION_POOL_CLASS', 'redis.ConnectionPool') pool_class = self.options.get('CONNECTION_POOL_CLASS', 'redis.ConnectionPool')
module_name, class_name = pool_class.rsplit('.', 1) module_name, class_name = pool_class.rsplit('.', 1)
...@@ -153,6 +157,7 @@ class BaseRedisCache(BaseCache): ...@@ -153,6 +157,7 @@ class BaseRedisCache(BaseCache):
server, server,
db=self.db, db=self.db,
password=self.password, password=self.password,
socket_timeout=self.socket_timeout,
) )
client = redis.Redis(**kwargs) client = redis.Redis(**kwargs)
kwargs.update( kwargs.update(
......
...@@ -30,6 +30,7 @@ class CacheConnectionPool(object): ...@@ -30,6 +30,7 @@ class CacheConnectionPool(object):
unix_socket_path=None, unix_socket_path=None,
connection_pool_class=None, connection_pool_class=None,
connection_pool_class_kwargs=None, connection_pool_class_kwargs=None,
socket_timeout=None,
**kwargs **kwargs
): ):
connection_identifier = (host, port, db, unix_socket_path) connection_identifier = (host, port, db, unix_socket_path)
...@@ -48,6 +49,7 @@ class CacheConnectionPool(object): ...@@ -48,6 +49,7 @@ class CacheConnectionPool(object):
'password': password, 'password': password,
'connection_class': connection_class, 'connection_class': connection_class,
'parser_class': parser_class, 'parser_class': parser_class,
'socket_timeout': socket_timeout,
} }
kwargs.update(connection_pool_class_kwargs) kwargs.update(connection_pool_class_kwargs)
......
# -*- coding: utf-8 -*-
try:
from django.test import override_settings
except ImportError:
from django.test.utils import override_settings
from django.test import TestCase
from redis.exceptions import ConnectionError
from tests.testapp.tests.base_tests import SetupMixin
LOCATION = "127.0.0.1:6382"
@override_settings(
CACHES={
'default': {
'BACKEND': 'redis_cache.RedisCache',
'LOCATION': LOCATION,
'OPTIONS': {
'DB': 15,
'PASSWORD': 'yadayada',
'PARSER_CLASS': 'redis.connection.HiredisParser',
'PICKLE_VERSION': -1,
'SOCKET_TIMEOUT': 0,
},
},
}
)
class SocketTimeoutTestCase(SetupMixin, TestCase):
def tearDown(self):
pass
def test_socket_timeout(self):
self.reset_pool()
cache = self.get_cache()
with self.assertRaises(ConnectionError):
cache.set('aaaaa', 'a')
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment