Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache resiliency #63

Merged
merged 11 commits into from
Oct 1, 2020
29 changes: 29 additions & 0 deletions msal_extensions/cache_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@
import os
import sys
import errno
import time
import logging

logger = logging.getLogger(__name__)

import portalocker
from distutils.version import LooseVersion

current_time = getattr(time, "monotonic", time.time)


class CrossPlatLock(object):
"""Offers a mechanism for waiting until another process is finished interacting with a shared
Expand All @@ -25,7 +32,29 @@ def __init__(self, lockfile_path):
flags=portalocker.LOCK_EX | portalocker.LOCK_NB,
**open_kwargs)

def try_to_create_lock_file(self):
timeout = 5
check_interval = 0.25
rayluo marked this conversation as resolved.
Show resolved Hide resolved
timeout_end = current_time() + timeout
while timeout_end > current_time():
try:
with open(self._lockpath, 'x'):
return True
except OSError as err:
if err.errno == errno.EEXIST:
rayluo marked this conversation as resolved.
Show resolved Hide resolved
logger.warning("Lock file exists, trying again after some time")
time.sleep(check_interval)
else:
raise
except ValueError:
logger.warning("Python 2 does not support atomic creation of file")
return False
rayluo marked this conversation as resolved.
Show resolved Hide resolved

return False

def __enter__(self):
if not self.try_to_create_lock_file():
logger.warning("Failed to create lock file")
file_handle = self._lock.__enter__()
file_handle.write('{} {}'.format(os.getpid(), sys.argv[0]).encode('utf-8'))
return file_handle
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cache_lock_file_perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_lock_for_high_workload(temp_location):


def test_lock_for_timeout(temp_location):
num_of_processes = 10
num_of_processes = 30
sleep_interval = 1
_run_multiple_processes(num_of_processes, temp_location, sleep_interval)
count = _validate_result_in_cache(temp_location)
Expand Down