Revert change to use monotonic as it doesnt work for cross process sync

This commit is contained in:
Kovid Goyal 2022-08-01 08:01:01 +05:30
parent 0fda25f369
commit c95cf4f9e5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -25,7 +25,7 @@ from calibre.ebooks.chardet import xml_to_unicode
from calibre.utils.lock import ExclusiveFile from calibre.utils.lock import ExclusiveFile
from calibre.utils.random_ua import accept_header_for_ua from calibre.utils.random_ua import accept_header_for_ua
current_version = (1, 1, 1) current_version = (1, 2, 0)
minimum_calibre_version = (2, 80, 0) minimum_calibre_version = (2, 80, 0)
webcache = {} webcache = {}
webcache_lock = Lock() webcache_lock = Lock()
@ -36,13 +36,15 @@ Result = namedtuple('Result', 'url title cached_url')
@contextmanager @contextmanager
def rate_limit(name='test', time_between_visits=2, max_wait_seconds=5 * 60, sleep_time=0.2): def rate_limit(name='test', time_between_visits=2, max_wait_seconds=5 * 60, sleep_time=0.2):
lock_file = os.path.join(cache_dir(), 'search-engines.' + name + '.lock') lock_file = os.path.join(cache_dir(), 'search-engine.' + name + '.lock')
with ExclusiveFile(lock_file, timeout=max_wait_seconds, sleep_time=sleep_time) as f: with ExclusiveFile(lock_file, timeout=max_wait_seconds, sleep_time=sleep_time) as f:
try: try:
lv = float(f.read().decode('utf-8').strip()) lv = float(f.read().decode('utf-8').strip())
except Exception: except Exception:
lv = 0 lv = 0
delta = time.monotonic() - lv # we cannot use monotonic() as this is cross process and historical
# data as well
delta = time.time() - lv
if delta < time_between_visits: if delta < time_between_visits:
time.sleep(time_between_visits - delta) time.sleep(time_between_visits - delta)
try: try:
@ -50,7 +52,7 @@ def rate_limit(name='test', time_between_visits=2, max_wait_seconds=5 * 60, slee
finally: finally:
f.seek(0) f.seek(0)
f.truncate() f.truncate()
f.write(repr(time.monotonic()).encode('utf-8')) f.write(repr(time.time()).encode('utf-8'))
def tostring(elem): def tostring(elem):