Reduce duplication of code in handling multi-type blocking argument
The 'blocking' argument typically either means blocking as a bool or blocking true but with the given timeout, so in various drivers they handle this multi-type argument in nearly the same manner. To unify that usage we can just have a utils method that breaks the multi-type argument into its components and uniformly use that in drivers. Change-Id: I0a6c4b50919fab72d383b9c9f2e772fc931cb6b8
This commit is contained in:
parent
f9a9a18b1c
commit
963bba02a0
|
@ -74,10 +74,7 @@ class FileLock(locking.Lock):
|
||||||
return self.acquired
|
return self.acquired
|
||||||
|
|
||||||
def acquire(self, blocking=True):
|
def acquire(self, blocking=True):
|
||||||
timeout = None
|
blocking, timeout = utils.convert_blocking(blocking)
|
||||||
if not isinstance(blocking, bool):
|
|
||||||
timeout = float(blocking)
|
|
||||||
blocking = True
|
|
||||||
watch = timeutils.StopWatch(duration=timeout)
|
watch = timeutils.StopWatch(duration=timeout)
|
||||||
watch.start()
|
watch.start()
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -64,19 +64,15 @@ class IPCLock(locking.Lock):
|
||||||
self._lock = None
|
self._lock = None
|
||||||
|
|
||||||
def acquire(self, blocking=True):
|
def acquire(self, blocking=True):
|
||||||
if (blocking is not True
|
if (blocking is not True and
|
||||||
and sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
|
sysv_ipc.SEMAPHORE_TIMEOUT_SUPPORTED is False):
|
||||||
raise tooz.NotImplemented(
|
raise tooz.NotImplemented("This system does not support"
|
||||||
"This system does not support semaphore timeout")
|
" semaphore timeouts")
|
||||||
# Convert blocking argument to a valid timeout value
|
blocking, timeout = utils.convert_blocking(blocking)
|
||||||
if blocking is True:
|
start_time = None
|
||||||
timeout = None
|
if not blocking:
|
||||||
start_time = None
|
|
||||||
elif blocking is False:
|
|
||||||
timeout = 0
|
timeout = 0
|
||||||
start_time = None
|
elif blocking and timeout is not None:
|
||||||
else:
|
|
||||||
timeout = blocking
|
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -74,14 +74,10 @@ class RedisLock(locking.Lock):
|
||||||
return owner_tok == lock_tok
|
return owner_tok == lock_tok
|
||||||
|
|
||||||
def acquire(self, blocking=True):
|
def acquire(self, blocking=True):
|
||||||
if blocking is True or blocking is False:
|
blocking, timeout = utils.convert_blocking(blocking)
|
||||||
blocking_timeout = None
|
|
||||||
else:
|
|
||||||
blocking_timeout = float(blocking)
|
|
||||||
blocking = True
|
|
||||||
with _translate_failures():
|
with _translate_failures():
|
||||||
self.acquired = self._lock.acquire(
|
self.acquired = self._lock.acquire(
|
||||||
blocking=blocking, blocking_timeout=blocking_timeout)
|
blocking=blocking, blocking_timeout=timeout)
|
||||||
if self.acquired:
|
if self.acquired:
|
||||||
self._coord._acquired_locks.add(self)
|
self._coord._acquired_locks.add(self)
|
||||||
return self.acquired
|
return self.acquired
|
||||||
|
|
|
@ -56,11 +56,7 @@ class ZooKeeperLock(locking.Lock):
|
||||||
cause=e)
|
cause=e)
|
||||||
|
|
||||||
def acquire(self, blocking=True):
|
def acquire(self, blocking=True):
|
||||||
if isinstance(blocking, bool):
|
blocking, timeout = utils.convert_blocking(blocking)
|
||||||
timeout = None
|
|
||||||
else:
|
|
||||||
blocking = True
|
|
||||||
timeout = float(blocking)
|
|
||||||
self.acquired = self._lock.acquire(blocking=blocking,
|
self.acquired = self._lock.acquire(blocking=blocking,
|
||||||
timeout=timeout)
|
timeout=timeout)
|
||||||
return self.acquired
|
return self.acquired
|
||||||
|
|
|
@ -104,6 +104,15 @@ def safe_abs_path(rooted_at, *pieces):
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def convert_blocking(blocking):
|
||||||
|
"""Converts a multi-type blocking variable into its derivatives."""
|
||||||
|
timeout = None
|
||||||
|
if not isinstance(blocking, bool):
|
||||||
|
timeout = float(blocking)
|
||||||
|
blocking = True
|
||||||
|
return blocking, timeout
|
||||||
|
|
||||||
|
|
||||||
def ensure_tree(path):
|
def ensure_tree(path):
|
||||||
"""Create a directory (and any ancestor directories required).
|
"""Create a directory (and any ancestor directories required).
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue