Skip to content

Commit b07f2c2

Browse files
committed
ongoing
1 parent 51e2bde commit b07f2c2

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def holders_key(self) -> str:
150150
@property
151151
def holder_key(self) -> str:
152152
"""Redis key for this instance's holder entry."""
153-
return f"{SEMAPHORE_HOLDER_KEY_PREFIX}{self.key}:{self.instance_id}"
153+
return f"{SEMAPHORE_KEY_PREFIX}{self.key}:holders:{self.instance_id}"
154154

155155
@computed_field
156156
@property
@@ -191,8 +191,10 @@ async def acquire(self) -> bool:
191191
ttl_seconds = int(self.ttl.total_seconds())
192192
blocking_timeout_seconds = 1
193193
if self.blocking:
194-
blocking_timeout_seconds = int(
195-
self.blocking_timeout.total_seconds() if self.blocking_timeout else 0
194+
blocking_timeout_seconds = (
195+
int(self.blocking_timeout.total_seconds())
196+
if self.blocking_timeout
197+
else 60
196198
)
197199

198200
# Execute the Lua scripts atomically
@@ -217,11 +219,9 @@ async def acquire(self) -> bool:
217219
self.key,
218220
self.instance_id,
219221
)
220-
if self.blocking:
221-
raise SemaphoreAcquisitionError(
222-
name=self.key, capacity=self.capacity
223-
) from e
224-
return False
222+
raise SemaphoreAcquisitionError(
223+
name=self.key, capacity=self.capacity
224+
) from e
225225

226226
assert len(tokens_key_token) == 2 # nosec
227227
assert tokens_key_token[0] == self.tokens_key # nosec
@@ -231,7 +231,7 @@ async def acquire(self) -> bool:
231231
result = await cls.acquire_script( # pylint: disable=not-callable
232232
keys=[self.holders_key, self.holder_key],
233233
args=[
234-
token[0],
234+
token,
235235
self.instance_id,
236236
ttl_seconds,
237237
],

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,10 @@ async def limited_function() -> None:
276276
key=semaphore_name,
277277
capacity=1,
278278
blocking=False,
279-
blocking_timeout=datetime.timedelta(seconds=0.1),
279+
blocking_timeout=None,
280280
)
281281
async def limited_function_non_blocking() -> None:
282-
await asyncio.sleep(0.5)
282+
await asyncio.sleep(2)
283283

284284
tasks = [asyncio.create_task(limited_function_non_blocking()) for _ in range(3)]
285285
results = await asyncio.gather(*tasks, return_exceptions=True)
@@ -424,6 +424,7 @@ async def limited_function() -> None:
424424
assert "longer than expected" in caplog.messages[-1]
425425

426426

427+
@pytest.mark.skip
427428
async def test_semaphore_fair_queuing(
428429
redis_client_sdk: RedisClientSDK,
429430
semaphore_name: str,
@@ -498,6 +499,7 @@ async def test_context_manager_capacity_enforcement(
498499
redis_client_sdk,
499500
key=semaphore_name,
500501
capacity=2,
502+
blocking_timeout=None,
501503
)
502504
@asynccontextmanager
503505
async def limited_context_manager():

0 commit comments

Comments
 (0)