@@ -100,14 +100,18 @@ class DistributedSemaphore(BaseModel):
100100 ),
101101 ] = DEFAULT_FACTORY
102102
103- # Private state attributes (not part of the model)
103+ # Class and/or Private state attributes (not part of the model)
104104 acquire_script : ClassVar [AsyncScript | None ] = None
105105 count_script : ClassVar [AsyncScript | None ] = None
106106 release_script : ClassVar [AsyncScript | None ] = None
107107 renew_script : ClassVar [AsyncScript | None ] = None
108108
109109 @classmethod
110110 def _register_scripts (cls , redis_client : RedisClientSDK ) -> None :
111+ """Register Lua scripts with Redis if not already done.
112+ This is done once per class, not per instance. Internally the Redis client
113+ caches the script SHA, so this is efficient. Even if called multiple times,
114+ the script is only registered once."""
111115 if cls .acquire_script is None :
112116 cls .acquire_script = redis_client .redis .register_script (
113117 ACQUIRE_SEMAPHORE_SCRIPT
@@ -206,7 +210,7 @@ async def release(self) -> None:
206210 # Execute the release Lua script atomically
207211 cls = type (self )
208212 assert cls .release_script is not None # nosec
209- result = await cls .release_script (
213+ result = await cls .release_script ( # pylint: disable=not-callable
210214 keys = (
211215 self .semaphore_key ,
212216 self .holder_key ,
@@ -243,7 +247,7 @@ async def _try_acquire(self) -> bool:
243247 # Execute the Lua script atomically
244248 cls = type (self )
245249 assert cls .acquire_script is not None # nosec
246- result = await cls .acquire_script (
250+ result = await cls .acquire_script ( # pylint: disable=not-callable
247251 keys = (self .semaphore_key , self .holder_key ),
248252 args = (self .instance_id , str (self .capacity ), str (ttl_seconds )),
249253 client = self .redis_client .redis ,
@@ -288,7 +292,7 @@ async def reacquire(self) -> None:
288292 # Execute the renewal Lua script atomically
289293 cls = type (self )
290294 assert cls .renew_script is not None # nosec
291- result = await cls .renew_script (
295+ result = await cls .renew_script ( # pylint: disable=not-callable
292296 keys = (self .semaphore_key , self .holder_key ),
293297 args = (
294298 self .instance_id ,
@@ -338,7 +342,7 @@ async def get_current_count(self) -> int:
338342 # Execute the count Lua script atomically
339343 cls = type (self )
340344 assert cls .count_script is not None # nosec
341- result = await cls .count_script (
345+ result = await cls .count_script ( # pylint: disable=not-callable
342346 keys = (self .semaphore_key ,),
343347 args = (str (ttl_seconds ),),
344348 client = self .redis_client .redis ,
0 commit comments