Limit duration of file locks
This prevents starvation of writers in other processes
This commit is contained in:
parent
14daa88772
commit
6b1acd14d7
@ -92,6 +92,7 @@ def load(configuration, logger):
|
|||||||
|
|
||||||
|
|
||||||
MIMETYPES = {"VADDRESSBOOK": "text/vcard", "VCALENDAR": "text/calendar"}
|
MIMETYPES = {"VADDRESSBOOK": "text/vcard", "VCALENDAR": "text/calendar"}
|
||||||
|
MAX_FILE_LOCK_DURATION = 0.25
|
||||||
|
|
||||||
|
|
||||||
def get_etag(text):
|
def get_etag(text):
|
||||||
@ -528,6 +529,7 @@ class Collection(BaseCollection):
|
|||||||
_waiters = []
|
_waiters = []
|
||||||
_lock_file = None
|
_lock_file = None
|
||||||
_lock_file_locked = False
|
_lock_file_locked = False
|
||||||
|
_lock_file_time = 0
|
||||||
_readers = 0
|
_readers = 0
|
||||||
_writer = False
|
_writer = False
|
||||||
|
|
||||||
@ -535,6 +537,11 @@ class Collection(BaseCollection):
|
|||||||
@contextmanager
|
@contextmanager
|
||||||
def acquire_lock(cls, mode):
|
def acquire_lock(cls, mode):
|
||||||
def condition():
|
def condition():
|
||||||
|
# Prevent starvation of writers in other processes
|
||||||
|
if cls._lock_file_locked:
|
||||||
|
time_delta = time.time() - cls._lock_file_time
|
||||||
|
if time_delta < 0 or time_delta > MAX_FILE_LOCK_DURATION:
|
||||||
|
return False
|
||||||
if mode == "r":
|
if mode == "r":
|
||||||
return not cls._writer
|
return not cls._writer
|
||||||
else:
|
else:
|
||||||
@ -588,6 +595,7 @@ class Collection(BaseCollection):
|
|||||||
except OSError:
|
except OSError:
|
||||||
cls.logger.debug("Locking not supported")
|
cls.logger.debug("Locking not supported")
|
||||||
cls._lock_file_locked = True
|
cls._lock_file_locked = True
|
||||||
|
cls._lock_file_time = time.time()
|
||||||
yield
|
yield
|
||||||
with cls._lock:
|
with cls._lock:
|
||||||
if mode == "r":
|
if mode == "r":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user