Skip to content

Cache resiliency #63

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

Merged
merged 11 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions msal_extensions/cache_lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
import os
import sys
import errno
import time
import logging

import portalocker
from distutils.version import LooseVersion

Expand All @@ -25,7 +28,24 @@ def __init__(self, lockfile_path):
flags=portalocker.LOCK_EX | portalocker.LOCK_NB,
**open_kwargs)

def try_to_create_lock_file(self):
retries_no = 100
retry_delay_milliseconds = 0.150
for i in range(retries_no):
try:
with open(self._lockpath, 'x'):
return True
except OSError as err:
if err.errno == errno.EEXIST:
time.sleep(retry_delay_milliseconds)
except ValueError:
logging.warning("Python 2 does not support atomic creation of file")
return False
return False

def __enter__(self):
if not self.try_to_create_lock_file():
logging.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