Skip to content

Commit

Permalink
Merge pull request #67 from jakm/timeouts
Browse files Browse the repository at this point in the history
added connectTimeout and replyTimeout
  • Loading branch information
fiorix committed Aug 1, 2014
2 parents bb277f5 + b385156 commit 39c5874
Showing 1 changed file with 77 additions and 43 deletions.
120 changes: 77 additions & 43 deletions txredisapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -2062,21 +2065,23 @@ 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
else:
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)
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand All @@ -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__ = [
Expand Down

0 comments on commit 39c5874

Please sign in to comment.