Skip to content

Commit f64c7f9

Browse files
committed
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.
1 parent b13d6fc commit f64c7f9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

memcache.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,9 @@ def check_key(self, key, key_extra_len=0):
13301330
"Control/space characters not allowed (key=%r)" % key)
13311331

13321332

1333+
_host_last_deaduntils = {}
1334+
1335+
13331336
class _Host(object):
13341337

13351338
def __init__(self, host, debug=0, dead_retry=_DEAD_RETRY,
@@ -1371,7 +1374,7 @@ def __init__(self, host, debug=0, dead_retry=_DEAD_RETRY,
13711374
self.port = int(hostData.get('port') or 11211)
13721375
self.address = (self.ip, self.port)
13731376

1374-
self.deaduntil = 0
1377+
self.deaduntil = _host_last_deaduntils.get(self.ip, 0)
13751378
self.socket = None
13761379
self.flush_on_next_connect = 0
13771380

@@ -1395,6 +1398,7 @@ def connect(self):
13951398
def mark_dead(self, reason):
13961399
self.debuglog("MemCache: %s: %s. Marking dead." % (self, reason))
13971400
self.deaduntil = time.time() + self.dead_retry
1401+
13981402
if self.flush_on_reconnect:
13991403
self.flush_on_next_connect = 1
14001404
self.close_socket()
@@ -1411,6 +1415,7 @@ def _get_socket(self):
14111415
s.connect(self.address)
14121416
except socket.timeout as msg:
14131417
self.mark_dead("connect: %s" % msg)
1418+
_host_last_deaduntils[self.ip] = self.deaduntil
14141419
return None
14151420
except socket.error as msg:
14161421
if isinstance(msg, tuple):

0 commit comments

Comments
 (0)