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

Rename redlock() decorator to synchronize() #305

Merged
merged 1 commit into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pottery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from .hyper import HyperLogLog # isort:skip
from .nextid import NextId # isort:skip
from .redlock import Redlock # isort:skip
from .redlock import redlock # isort:skip
from .redlock import synchronize # isort:skip
from .timer import ContextTimer # isort:skip

from .counter import RedisCounter
Expand All @@ -66,7 +66,7 @@
'HyperLogLog',
'NextId',
'Redlock',
'redlock',
'synchronize',
'ContextTimer',

'RedisCounter',
Expand Down
16 changes: 8 additions & 8 deletions pottery/redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,26 +551,26 @@ def __repr__(self) -> str:
)


def redlock(*,
key: str,
masters: Iterable[Redis] = frozenset(),
auto_release_time: int = AUTO_RELEASE_TIME,
) -> Callable[[F], F]:
def synchronize(*,
key: str,
masters: Iterable[Redis] = frozenset(),
auto_release_time: int = AUTO_RELEASE_TIME,
) -> Callable[[F], F]:
def decorator(func: F) -> F:
@functools.wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
redlock_ = Redlock(
redlock = Redlock(
key=key,
masters=masters,
auto_release_time=auto_release_time,
)
with ContextTimer() as timer, redlock_:
with ContextTimer() as timer, redlock:
return_value = func(*args, **kwargs)

_logger.info(
'%s() held %s for %d ms',
func.__qualname__,
redlock_.key,
redlock.key,
timer.elapsed(),
)
return return_value
Expand Down
23 changes: 10 additions & 13 deletions tests/test_redlock.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pottery import Redlock
from pottery import ReleaseUnlockedLock
from pottery import TooManyExtensions
from pottery import redlock
from pottery import synchronize
from tests.base import TestCase # type: ignore


Expand Down Expand Up @@ -180,22 +180,19 @@ def test_repr(self):
"<Redlock key=redlock:printer value=b'' timeout=0>"


class RedlockDecoratorTests(TestCase):
def setUp(self):
super().setUp()

def expensive_method(*args, **kwargs):
class SynchronizeTests(TestCase):
def test_synchronize(self):
@synchronize(
key='expensive-func',
masters={self.redis},
auto_release_time=1500,
)
def expensive_func():
time.sleep(1)
return time.time()

self.expensive_method = redlock(
key='expensive-method',
masters={self.redis},
)(expensive_method)

def test_redlock_decorator(self):
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(self.expensive_method) for _ in range(3)]
futures = [executor.submit(expensive_func) for _ in range(3)]
results = sorted(future.result() for future in futures)
for result1, result2 in zip(results, results[1:]):
delta = result2 - result1
Expand Down