diff --git a/txredisapi.py b/txredisapi.py index af7d7b3..46e9858 100644 --- a/txredisapi.py +++ b/txredisapi.py @@ -1958,7 +1958,8 @@ class RedisFactory(protocol.ReconnectingClientFactory): protocol = RedisProtocol def __init__(self, uuid, dbid, poolsize, isLazy=False, - handler=ConnectionHandler, charset="utf-8", password=None): + handler=ConnectionHandler, charset="utf-8", password=None, + replyTimeout=None): if not isinstance(poolsize, int): raise ValueError("Redis poolsize must be an integer, not %s" % repr(poolsize)) @@ -1973,6 +1974,7 @@ def __init__(self, uuid, dbid, poolsize, isLazy=False, self.isLazy = isLazy self.charset = charset self.password = password + self.replyTimeout = replyTimeout self.idx = 0 self.size = 0 @@ -1988,6 +1990,7 @@ def buildProtocol(self, addr): else: p = self.protocol() p.factory = self + p.timeOut = self.replyTimeout return p def addConnection(self, conn): @@ -2062,13 +2065,14 @@ def __init__(self, isLazy=False, handler=ConnectionHandler): handler=handler) -def makeConnection(host, port, dbid, poolsize, reconnect, isLazy, charset, password): +def makeConnection(host, port, dbid, poolsize, reconnect, isLazy, + charset, password, connectTimeout, replyTimeout): uuid = "%s:%s" % (host, port) factory = RedisFactory(uuid, dbid, poolsize, isLazy, ConnectionHandler, - charset, password) + charset, password, replyTimeout) factory.continueTrying = reconnect for x in xrange(poolsize): - reactor.connectTCP(host, port, factory) + reactor.connectTCP(host, port, factory, connectTimeout) if isLazy: return factory.handler @@ -2076,7 +2080,8 @@ def makeConnection(host, port, dbid, poolsize, reconnect, isLazy, charset, passw return factory.deferred -def makeShardedConnection(hosts, dbid, poolsize, reconnect, isLazy, charset, password): +def makeShardedConnection(hosts, dbid, poolsize, reconnect, isLazy, + charset, password, connectTimeout, replyTimeout): err = "Please use a list or tuple of host:port for sharded connections" if not isinstance(hosts, (list, tuple)): raise ValueError(err) @@ -2090,7 +2095,7 @@ def makeShardedConnection(hosts, dbid, poolsize, reconnect, isLazy, charset, pas raise ValueError(err) c = makeConnection(host, port, dbid, poolsize, reconnect, isLazy, - charset, password) + charset, password, connectTimeout, replyTimeout) connections.append(c) if isLazy: @@ -2102,52 +2107,67 @@ def makeShardedConnection(hosts, dbid, poolsize, reconnect, isLazy, charset, pas def Connection(host="localhost", port=6379, dbid=None, reconnect=True, - charset="utf-8", password=None): - return makeConnection(host, port, dbid, 1, reconnect, False, charset, password) + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeConnection(host, port, dbid, 1, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyConnection(host="localhost", port=6379, dbid=None, reconnect=True, - charset="utf-8", password=None): - return makeConnection(host, port, dbid, 1, reconnect, True, charset, password) + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeConnection(host, port, dbid, 1, reconnect, True, + charset, password, connectTimeout, replyTimeout) def ConnectionPool(host="localhost", port=6379, dbid=None, - poolsize=10, reconnect=True, charset="utf-8", password=None): - return makeConnection(host, port, dbid, poolsize, reconnect, False, charset, password) + poolsize=10, reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeConnection(host, port, dbid, poolsize, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyConnectionPool(host="localhost", port=6379, dbid=None, - poolsize=10, reconnect=True, charset="utf-8", password=None): - return makeConnection(host, port, dbid, poolsize, reconnect, True, charset, password) + poolsize=10, reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeConnection(host, port, dbid, poolsize, reconnect, True, + charset, password, connectTimeout, replyTimeout) -def ShardedConnection(hosts, dbid=None, reconnect=True, charset="utf-8", password=None): - return makeShardedConnection(hosts, dbid, 1, reconnect, False, charset, password) +def ShardedConnection(hosts, dbid=None, reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeShardedConnection(hosts, dbid, 1, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyShardedConnection(hosts, dbid=None, reconnect=True, charset="utf-8", - password=None): - return makeShardedConnection(hosts, dbid, 1, reconnect, True, charset, password) + password=None, + connectTimeout=None, replyTimeout=None): + return makeShardedConnection(hosts, dbid, 1, reconnect, True, + charset, password, connectTimeout, replyTimeout) def ShardedConnectionPool(hosts, dbid=None, poolsize=10, reconnect=True, - charset="utf-8", password=None): + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): return makeShardedConnection(hosts, dbid, poolsize, reconnect, False, - charset, password) + charset, password, connectTimeout, replyTimeout) def lazyShardedConnectionPool(hosts, dbid=None, poolsize=10, reconnect=True, - charset="utf-8", password=None): + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): return makeShardedConnection(hosts, dbid, poolsize, reconnect, True, - charset, password) + charset, password, connectTimeout, replyTimeout) -def makeUnixConnection(path, dbid, poolsize, reconnect, isLazy, charset, password): +def makeUnixConnection(path, dbid, poolsize, reconnect, isLazy, + charset, password, connectTimeout, replyTimeout): factory = RedisFactory(path, dbid, poolsize, isLazy, UnixConnectionHandler, - charset, password) + charset, password, replyTimeout) factory.continueTrying = reconnect for x in xrange(poolsize): - reactor.connectUNIX(path, factory) + reactor.connectUNIX(path, factory, connectTimeout) if isLazy: return factory.handler @@ -2156,14 +2176,15 @@ def makeUnixConnection(path, dbid, poolsize, reconnect, isLazy, charset, passwor def makeShardedUnixConnection(paths, dbid, poolsize, reconnect, isLazy, - charset, password): + charset, password, connectTimeout, replyTimeout): err = "Please use a list or tuple of paths for sharded unix connections" if not isinstance(paths, (list, tuple)): raise ValueError(err) connections = [] for path in paths: - c = makeUnixConnection(path, dbid, poolsize, reconnect, isLazy, charset, password) + c = makeUnixConnection(path, dbid, poolsize, reconnect, isLazy, + charset, password, connectTimeout, replyTimeout) connections.append(c) if isLazy: @@ -2175,45 +2196,58 @@ def makeShardedUnixConnection(paths, dbid, poolsize, reconnect, isLazy, def UnixConnection(path="/tmp/redis.sock", dbid=None, reconnect=True, - charset="utf-8", password=None): - return makeUnixConnection(path, dbid, 1, reconnect, False, charset, password) + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeUnixConnection(path, dbid, 1, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyUnixConnection(path="/tmp/redis.sock", dbid=None, reconnect=True, - charset="utf-8", password=None): - return makeUnixConnection(path, dbid, 1, reconnect, True, charset, password) + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeUnixConnection(path, dbid, 1, reconnect, True, + charset, password, connectTimeout, replyTimeout) def UnixConnectionPool(path="/tmp/redis.sock", dbid=None, poolsize=10, - reconnect=True, charset="utf-8", password=None): - return makeUnixConnection(path, dbid, poolsize, reconnect, False, charset, password) + reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeUnixConnection(path, dbid, poolsize, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyUnixConnectionPool(path="/tmp/redis.sock", dbid=None, poolsize=10, - reconnect=True, charset="utf-8", password=None): - return makeUnixConnection(path, dbid, poolsize, reconnect, True, charset, password) + reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeUnixConnection(path, dbid, poolsize, reconnect, True, + charset, password, connectTimeout, replyTimeout) def ShardedUnixConnection(paths, dbid=None, reconnect=True, charset="utf-8", - password=None): - return makeShardedUnixConnection(paths, dbid, 1, reconnect, False, charset, password) + password=None, connectTimeout=None, replyTimeout=None): + return makeShardedUnixConnection(paths, dbid, 1, reconnect, False, + charset, password, connectTimeout, replyTimeout) def lazyShardedUnixConnection(paths, dbid=None, reconnect=True, - charset="utf-8", password=None): - return makeShardedUnixConnection(paths, dbid, 1, reconnect, True, charset, password) + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): + return makeShardedUnixConnection(paths, dbid, 1, reconnect, True, + charset, password, connectTimeout, replyTimeout) def ShardedUnixConnectionPool(paths, dbid=None, poolsize=10, reconnect=True, - charset="utf-8", password=None): + charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): return makeShardedUnixConnection(paths, dbid, poolsize, reconnect, False, - charset, password) + charset, password, connectTimeout, replyTimeout) def lazyShardedUnixConnectionPool(paths, dbid=None, poolsize=10, - reconnect=True, charset="utf-8", password=None): + reconnect=True, charset="utf-8", password=None, + connectTimeout=None, replyTimeout=None): return makeShardedUnixConnection(paths, dbid, poolsize, reconnect, True, - charset, password) + charset, password, connectTimeout, replyTimeout) __all__ = [