File tree Expand file tree Collapse file tree 2 files changed +16
-1955
lines changed Expand file tree Collapse file tree 2 files changed +16
-1955
lines changed Original file line number Diff line number Diff line change @@ -84,6 +84,10 @@ async def __aexit__(
8484 ) -> None :
8585 """Do nothing. We need this to ``__aenter__`` to work."""
8686
87+ async def redis_version (self ):
88+ version = await self ._backend .info (section = 'SERVER' )
89+ return version ['redis_version' ]
90+
8791 # Private API:
8892
8993 async def _acquire (self ) -> None :
@@ -95,7 +99,18 @@ async def _acquire(self) -> None:
9599 pipeline = self ._backend .pipeline ()
96100
97101 async with self ._lock :
98- current_rate = await self ._run_pipeline (cache_key , pipeline )
102+ redis_version = await self .redis_version ()
103+
104+ if int (redis_version .split ('.' )[0 ]) < 7 :
105+ current_rate , * _ = await pipeline .incr (cache_key ).execute ()
106+ if current_rate == 1 :
107+ await pipeline .execute_command (
108+ 'EXPIRE' ,
109+ cache_key ,
110+ self ._rate_spec .seconds ,
111+ ).execute ()
112+ else :
113+ current_rate = await self ._run_pipeline (cache_key , pipeline )
99114 # This looks like a coverage error on 3.10:
100115 if current_rate > self ._rate_spec .requests : # pragma: no cover
101116 raise RateLimitError ('Rate limit is hit' , current_rate )
You can’t perform that action at this time.
0 commit comments