-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2299 from coronasafe/sainak/fix/atomicity-of-cons…
…ultations Fix race condition in consultation creation
- Loading branch information
Showing
8 changed files
with
171 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from django.conf import settings | ||
from django.core.cache import cache | ||
from rest_framework.exceptions import APIException | ||
|
||
|
||
class ObjectLocked(APIException): | ||
status_code = 423 | ||
default_detail = "The resource you are trying to access is locked" | ||
default_code = "object_locked" | ||
|
||
|
||
class Lock: | ||
def __init__(self, key, timeout=settings.LOCK_TIMEOUT): | ||
self.key = f"lock:{key}" | ||
self.timeout = timeout | ||
|
||
def acquire(self): | ||
if not cache.set(self.key, True, self.timeout, nx=True): | ||
raise ObjectLocked() | ||
|
||
def release(self): | ||
return cache.delete(self.key) | ||
|
||
def __enter__(self): | ||
self.acquire() | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
self.release() | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.core.cache.backends import dummy, locmem | ||
from django.core.cache.backends.base import DEFAULT_TIMEOUT | ||
|
||
|
||
class DummyCache(dummy.DummyCache): | ||
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None, nx=None): | ||
super().set(key, value, timeout, version) | ||
# mimic the behavior of django_redis with setnx, for tests | ||
return True | ||
|
||
|
||
class LocMemCache(locmem.LocMemCache): | ||
def set(self, key, value, timeout=DEFAULT_TIMEOUT, version=None, nx=None): | ||
super().set(key, value, timeout, version) | ||
# mimic the behavior of django_redis with setnx, for tests | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters