Skip to content

Commit 46ebd48

Browse files
committed
getting there
1 parent 972431b commit 46ebd48

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

packages/service-library/src/servicelib/redis/_errors.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,23 @@ class LockLostError(BaseRedisError):
2626
ProjectLockError: TypeAlias = redis.exceptions.LockError # NOTE: backwards compatible
2727

2828

29-
class SemaphoreAcquisitionError(BaseRedisError):
29+
class SemaphoreError(BaseRedisError):
30+
msg_template: str = (
31+
"Unexpected error with semaphore '{name}' by this instance `{instance_id}`"
32+
)
33+
34+
35+
class SemaphoreAcquisitionError(SemaphoreError):
3036
msg_template: str = (
3137
"Could not acquire semaphore '{name}' by this instance `{instance_id}`"
3238
)
3339

3440

35-
class SemaphoreNotAcquiredError(BaseRedisError):
41+
class SemaphoreNotAcquiredError(SemaphoreError):
3642
msg_template: str = (
3743
"Semaphore '{name}' was not acquired by this instance `{instance_id}`"
3844
)
3945

4046

41-
class SemaphoreLostError(BaseRedisError):
47+
class SemaphoreLostError(SemaphoreError):
4248
msg_template: str = "Semaphore '{name}' was lost by this instance `{instance_id}`"

packages/service-library/src/servicelib/redis/_semaphore.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
)
4141
from ._errors import (
4242
SemaphoreAcquisitionError,
43+
SemaphoreError,
4344
SemaphoreLostError,
4445
SemaphoreNotAcquiredError,
4546
)
@@ -452,10 +453,10 @@ async def _periodic_reacquisition(
452453
if not started.is_set():
453454
started.set()
454455

456+
lock_acquisition_time = arrow.utcnow()
455457
try:
456458
if not await semaphore.acquire():
457459
raise SemaphoreAcquisitionError(name=key, instance_id=semaphore.instance_id)
458-
lock_acquisition_time = arrow.utcnow()
459460

460461
async with (
461462
asyncio.TaskGroup() as tg
@@ -471,15 +472,13 @@ async def _periodic_reacquisition(
471472

472473
await cancel_wait_task(auto_reacquisition_task)
473474
except BaseExceptionGroup as eg:
474-
semaphore_lost_errors, other_errors = eg.split(
475-
SemaphoreLostError | SemaphoreNotAcquiredError
476-
)
475+
semaphore_errors, other_errors = eg.split(SemaphoreError)
477476
if other_errors:
478477
assert len(other_errors.exceptions) == 1 # nosec
479-
raise other_errors from eg
480-
assert semaphore_lost_errors is not None # nosec
481-
assert len(semaphore_lost_errors.exceptions) == 1 # nosec
482-
raise semaphore_lost_errors.exceptions[0] from eg
478+
raise other_errors.exceptions[0] from eg
479+
assert semaphore_errors is not None # nosec
480+
assert len(semaphore_errors.exceptions) == 1 # nosec
481+
raise semaphore_errors.exceptions[0] from eg
483482
finally:
484483
try:
485484
await semaphore.release()

packages/service-library/tests/redis/test_semaphore.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ async def test_semaphore_context_manager_with_exception(
496496
captured_semaphore: DistributedSemaphore | None = None
497497

498498
async def _raising_context():
499-
async with DistributedSemaphore(
499+
async with distributed_semaphore(
500500
redis_client=redis_client_sdk,
501501
key=semaphore_name,
502502
capacity=semaphore_capacity,
@@ -524,10 +524,10 @@ async def test_multiple_semaphores_different_keys(
524524
capacity = 1
525525

526526
async with (
527-
DistributedSemaphore(
527+
distributed_semaphore(
528528
redis_client=redis_client_sdk, key=key1, capacity=capacity
529529
),
530-
DistributedSemaphore(
530+
distributed_semaphore(
531531
redis_client=redis_client_sdk, key=key2, capacity=capacity
532532
),
533533
):

0 commit comments

Comments
 (0)