2727from python_wrap import _core , _fork
2828from python_wrap ._backend import BackendMonitor
2929from python_wrap ._runtime import name_digest
30+ from python_wrap ._scripts import Script
3031from python_wrap .deadline import remaining as _deadline_remaining
3132from python_wrap .retry import RetryBudget , _compute_delay , http_status , retry , retry_after_header
3233from python_wrap .shared import _async_redis_client
@@ -358,6 +359,16 @@ def _pause_key(host: str) -> str:
358359 return "python-wrap:http-pause:" + name_digest (host )
359360
360361
362+ _EXTEND_PAUSE = """
363+ local ttl = tonumber(ARGV[1])
364+ if redis.call('PTTL', KEYS[1]) < ttl then
365+ redis.call('SET', KEYS[1], '1', 'PX', ttl)
366+ end
367+ return 1
368+ """
369+ _EXTEND_PAUSE_SCRIPT = Script (_EXTEND_PAUSE )
370+
371+
361372@dataclass (frozen = True )
362373class HttpPolicy :
363374 """Retries plus a circuit breaker, rate limiter, bulkhead and timeouts for HTTP calls.
@@ -405,9 +416,6 @@ class HttpPolicy:
405416 _blocked : OrderedDict [str , float ] = field (
406417 default_factory = OrderedDict , init = False , repr = False , compare = False
407418 )
408- _pause_checks : OrderedDict [str , float ] = field (
409- default_factory = OrderedDict , init = False , repr = False , compare = False
410- )
411419 _pause_monitor : BackendMonitor = field (
412420 default_factory = lambda : BackendMonitor ("http" , "pause" , "allow" , register = False ),
413421 init = False ,
@@ -528,18 +536,6 @@ def _pause_client(self) -> Any:
528536 shared = resolve_shared (self .shared )
529537 return None if shared is None else shared .client
530538
531- def _should_read_pause (self , host : str ) -> bool :
532- now = _core .monotonic ()
533- with self ._lock :
534- previous = self ._pause_checks .get (host )
535- if previous is not None and now - previous < 1 :
536- return False
537- self ._pause_checks [host ] = now
538- self ._pause_checks .move_to_end (host )
539- if len (self ._pause_checks ) > self .max_hosts :
540- self ._pause_checks .popitem (last = False )
541- return True
542-
543539 def _remember_pause (self , host : str , seconds : float ) -> None :
544540 if seconds <= 0 :
545541 return
@@ -552,7 +548,7 @@ def _remember_pause(self, host: str, seconds: float) -> None:
552548
553549 def _read_pause (self , host : str ) -> None :
554550 client = self ._pause_client ()
555- if client is not None and self . _should_read_pause ( host ) :
551+ if client is not None :
556552 millis = self ._pause_monitor .call (
557553 "pttl" , lambda : client .pttl (_pause_key (host )), lambda : 0
558554 )
@@ -564,7 +560,7 @@ async def _aread_pause(self, host: str) -> None:
564560 return
565561 if not _async_redis_client (client ):
566562 await asyncio .to_thread (self ._read_pause , host )
567- elif self . _should_read_pause ( host ) :
563+ else :
568564 millis = await self ._pause_monitor .acall (
569565 "pttl" , lambda : client .pttl (_pause_key (host )), lambda : 0
570566 )
@@ -578,7 +574,9 @@ def _note(self, host: str, response: Any) -> None:
578574 if client is not None :
579575 self ._pause_monitor .call (
580576 "set" ,
581- lambda : client .set (_pause_key (host ), "1" , px = math .ceil (hinted * 1000 )),
577+ lambda : _EXTEND_PAUSE_SCRIPT .run (
578+ client , 1 , _pause_key (host ), math .ceil (hinted * 1000 )
579+ ),
582580 lambda : None ,
583581 )
584582
@@ -595,7 +593,9 @@ async def _anote(self, host: str, response: Any) -> None:
595593 self ._remember_pause (host , hinted )
596594 await self ._pause_monitor .acall (
597595 "set" ,
598- lambda : client .set (_pause_key (host ), "1" , px = math .ceil (hinted * 1000 )),
596+ lambda : _EXTEND_PAUSE_SCRIPT .arun (
597+ client , 1 , _pause_key (host ), math .ceil (hinted * 1000 )
598+ ),
599599 lambda : None ,
600600 )
601601
0 commit comments