From 0d5281e2cef25b1ea962e74c2723a51a5e678da2 Mon Sep 17 00:00:00 2001 From: OctopusZhang Date: Sat, 11 Jun 2016 03:14:48 +0800 Subject: [PATCH 1/2] share deaduntils between hosts This will help avoid many socket timeout especially when we use python-memcached with eventlet. For example,When use eventlet,for each greenlet we call client.get first time,the client will be rebuild,and client.servers[*].deaduntil will be 0.For each http request,we will have one greenlet.So we get a socket timeout for each request. This could be fixed in application,but i believe it's better to fix it in python-memcached. --- memcache.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/memcache.py b/memcache.py index 9823a2f..a3f2804 100644 --- a/memcache.py +++ b/memcache.py @@ -1330,6 +1330,9 @@ def check_key(self, key, key_extra_len=0): "Control/space characters not allowed (key=%r)" % key) +_host_last_deaduntils = {} + + class _Host(object): def __init__(self, host, debug=0, dead_retry=_DEAD_RETRY, @@ -1371,7 +1374,7 @@ def __init__(self, host, debug=0, dead_retry=_DEAD_RETRY, self.port = int(hostData.get('port') or 11211) self.address = (self.ip, self.port) - self.deaduntil = 0 + self.deaduntil = _host_last_deaduntils.get(self.ip, 0) self.socket = None self.flush_on_next_connect = 0 @@ -1411,6 +1414,7 @@ def _get_socket(self): s.connect(self.address) except socket.timeout as msg: self.mark_dead("connect: %s" % msg) + _host_last_deaduntils[self.ip] = self.deaduntil return None except socket.error as msg: if isinstance(msg, tuple): From 23ec23bb40da98b75321afe13104421703ff096c Mon Sep 17 00:00:00 2001 From: Octopus zhang <1004988384@qq.com> Date: Sat, 17 Dec 2016 23:33:19 +0800 Subject: [PATCH 2/2] Update memcache.py --- memcache.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/memcache.py b/memcache.py index a3f2804..29f7946 100644 --- a/memcache.py +++ b/memcache.py @@ -1385,8 +1385,11 @@ def debuglog(self, str): sys.stderr.write("MemCached: %s\n" % str) def _check_dead(self): - if self.deaduntil and self.deaduntil > time.time(): - return 1 + if self.deaduntil: + if self.deaduntil > time.time(): + return 1 + else: + _host_last_deaduntils[self.ip] = 0 self.deaduntil = 0 return 0