memcached: use retrying rather than sleeping

That should allow better handling of retrying, sleep delays, etc.

Change-Id: I7658b4d5bacb0aef84a8c377a7e487327011b7ee
This commit is contained in:
Julien Danjou 2014-06-23 18:13:58 +02:00
parent 135a523a94
commit f438de0fdf
1 changed files with 11 additions and 13 deletions

View File

@ -17,7 +17,6 @@
# under the License. # under the License.
import collections import collections
import time
import msgpack import msgpack
import pymemcache.client import pymemcache.client
@ -50,19 +49,18 @@ class MemcachedLock(locking.Lock):
self.name = self._LOCK_PREFIX + name self.name = self._LOCK_PREFIX + name
self.timeout = timeout self.timeout = timeout
@retry
def acquire(self, blocking=True): def acquire(self, blocking=True):
while True: if self.coord.client.add(
if self.coord.client.add( self.name,
self.name, self.coord._member_id,
self.coord._member_id, expire=self.timeout,
expire=self.timeout, noreply=False):
noreply=False): self.coord._acquired_locks.append(self)
self.coord._acquired_locks.append(self) return True
return True if not blocking:
if not blocking: return False
return False raise Retry
# NOTE(jd) Configurable? :/
time.sleep(0.11)
def release(self): def release(self):
self.coord._acquired_locks.remove(self) self.coord._acquired_locks.remove(self)