IlyaSkriblovsky / txredisapi

non-blocking redis client for python twisted
Apache License 2.0
235 stars 91 forks source link

CLIENT SETNAME/CLIENT GETNAME #93

Open offero opened 8 years ago

offero commented 8 years ago

I'd like to be able to implement the CLIENT SETNAME and GETNAME operations, but it doesn't work well with a connection pool. What would be the current recommended way to make sure the client connections are named? Is there one?

offero commented 8 years ago

I made a Factory class that inherits from RedisFactory in order to issue the CLIENT SETNAME command in the addConnection function. I'll post a link to a snippet shortly.

offero commented 8 years ago
class ClientNamedRedisFactory(redis.RedisFactory):
    def __init__(self, name, *args, **kwargs):
        redis.RedisFactory.__init__(self, *args, **kwargs)
        self.name = name

    def addConnection(self, conn):
        conn.execute_command("CLIENT", "SETNAME", self.name)
        return redis.RedisFactory.addConnection(self, conn)

def makeConnection(url, name,
                    reconnect = True, charset = "utf-8",
                    isLazy = True, poolsize = 5,
                    replyTimeout = None,
                    convertNumbers = True,
                    connectTimeout = None):
    """A version of txredisapi.makeConnection that uses ClientNameRedisFactory
    instead of txredisapi.RedisFactory.
    """
    host, port, dbid, password = parse_url(url)
    uuid = "%s:%s" % (host, port)
    factory = ClientNameRedisFactory(name, uuid, int(dbid), poolsize, isLazy,
                            redis.ConnectionHandler, charset, password,
                            replyTimeout, convertNumbers)
    factory.continueTrying = reconnect
    for x in xrange(poolsize):
        reactor.connectTCP(host, port, factory, connectTimeout)

    if isLazy:
        return factory.handler
    else:
        return factory.deferred
fiorix commented 8 years ago

Can't really see what's going on from my mobile but looks like you want to send a PR?

On Feb 8, 2016, at 4:06 PM, Chris notifications@github.com wrote:

class ClientNamedRedisFactory(redis.RedisFactory): def init(self, name, _args, _kwargs): redis.RedisFactory.init(self, _args, _kwargs) self.name = name

def addConnection(self, conn):
    conn.execute_command("CLIENT", "SETNAME", self.name)
    return redis.RedisFactory.addConnection(self, conn)

def makeConnection(url, name, reconnect = True, charset = "utf-8", isLazy = True, poolsize = 5, replyTimeout = None, convertNumbers = True, connectTimeout = None): """A version of txredisapi.makeConnection that uses ClientNameRedisFactory instead of txredisapi.RedisFactory. """ host, port, dbid, password = parse_url(url) uuid = "%s:%s" % (host, port) factory = ClientNameRedisFactory(name, uuid, int(dbid), poolsize, isLazy, redis.ConnectionHandler, charset, password, replyTimeout, convertNumbers) factory.continueTrying = reconnect for x in xrange(poolsize): reactor.connectTCP(host, port, factory, connectTimeout)

if isLazy:
    return factory.handler
else:
    return factory.deferred

— Reply to this email directly or view it on GitHub.

IlyaSkriblovsky commented 8 years ago

Good idea. I think name might be added as the new optional argument to all [lazy][Sharded]Connection[Pool] functions. Could you please convert it to pull request?

offero commented 8 years ago

Sure.