@@ -464,11 +464,18 @@ def _mcall(
464464 func : Callable ,
465465 environ : dict ,
466466 start_response : Callable ,
467+ ip_throttle_secs : float | Callable ,
467468 memcachekey : str | Callable | None ,
468469 expire : int | Callable ,
469470 content_type : str | Callable ,
470471):
471472 """Call the function with memcachekey handling."""
473+ if ip_is_throttled (environ , ip_throttle_secs ):
474+ start_response (
475+ "429 Too Many Requests" ,
476+ [("Content-type" , "text/plain" )],
477+ )
478+ return b"Too many requests from your IP address, slow down."
472479 if memcachekey is None :
473480 return func (environ , start_response )
474481 key = memcachekey if isinstance (memcachekey , str ) else memcachekey (environ )
@@ -593,14 +600,6 @@ def _handle_exp(errormsg, routine=False, code=500):
593600 )
594601 return msg .encode ("ascii" , errors = "replace" )
595602
596- if ip_is_throttled (environ , kwargs .get ("ip_throttle_secs" , 0 )):
597- start_response (
598- "429 Too Many Requests" ,
599- [("Content-type" , "text/plain" )],
600- )
601- yield b"Too many requests from your IP address, slow down."
602- return
603-
604603 start_time = datetime .now (timezone .utc )
605604 status_code = 500
606605 try :
@@ -615,6 +614,7 @@ def _handle_exp(errormsg, routine=False, code=500):
615614 func ,
616615 environ ,
617616 start_response ,
617+ kwargs .get ("ip_throttle_secs" , 0 ),
618618 kwargs .get ("memcachekey" ),
619619 kwargs .get ("memcacheexpire" , 3600 ),
620620 kwargs .get ("content_type" , "application/json" ),
0 commit comments