Skip to content

Commit

Permalink
fix test of py2
Browse files Browse the repository at this point in the history
  • Loading branch information
Revolution1 committed May 8, 2019
1 parent 36be454 commit 4737015
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions etcd3/stateful/lease.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,35 @@
import threading
import time

import six

from ..errors import ErrLeaseNotFound
from ..utils import log
from ..utils import retry

if six.PY2: # pragma: no cover
def wait_lock(lock, timeout):
"""
Hack for python2.7 since it's lock not support timeout
:param lock: threading.Lock
:param timeout: seconds of timeout
:return: bool
"""
cond = threading.Condition(threading.Lock())
with cond:
current_time = start_time = time.time()
while current_time < start_time + timeout:
if lock.acquire(False):
return True
else:
cond.wait(timeout - current_time + start_time)
current_time = time.time()
return False
else:
def wait_lock(lock, timeout):
return lock.acquire(True, timeout=timeout)


class Lease(object):
def __init__(self, client, ttl, ID=0, new=True):
Expand Down Expand Up @@ -128,7 +153,8 @@ def keepalived():
for _ in range(int(self.grantedTTL / 2.0)): # keep per grantedTTL/4 seconds
if not self.keeping:
break
self._lock.acquire(True, timeout=0.5)
# self._lock.acquire(True, timeout=0.5)
wait_lock(self._lock, timeout=0.5)
log.debug("canceled keeping lease %d" % self.ID)
if cancel_cb:
try:
Expand All @@ -149,10 +175,9 @@ def cancel_keepalive(self, join=True):
"""
self.keeping = False
try:
self._lock.acquire(False)
finally:
self._lock.release()
except RuntimeError:
# release lock whatever
pass
if join and self._thread and self._thread.is_alive():
self._thread.join()

Expand Down

0 comments on commit 4737015

Please sign in to comment.