diff --git a/dank_mids/ENVIRONMENT_VARIABLES.py b/dank_mids/ENVIRONMENT_VARIABLES.py index 5e93ec40..c3b80fee 100644 --- a/dank_mids/ENVIRONMENT_VARIABLES.py +++ b/dank_mids/ENVIRONMENT_VARIABLES.py @@ -37,6 +37,8 @@ MAX_JSONRPC_BATCH_SIZE = _envs.create_env("MAX_JSONRPC_BATCH_SIZE", int, default=500) # Maximum amount of requests per second REQUESTS_PER_SECOND = _envs.create_env("REQUESTS_PER_SECOND", int, default=50) +# Minimum amount of requests per second after rate limit reduction +MIN_REQUESTS_PER_SECOND = _envs.create_env("MIN_REQUESTS_PER_SECOND", int, default=10) # Enable Demo Mode? demo_mode = _envs._deprecated_format.create_env("DEMO_MODE", bool, default=False, verbose=False) diff --git a/dank_mids/helpers/_session.py b/dank_mids/helpers/_session.py index 7234da31..5c3de08e 100644 --- a/dank_mids/helpers/_session.py +++ b/dank_mids/helpers/_session.py @@ -227,15 +227,16 @@ async def handle_too_many_requests(self, endpoint: str, error: ClientResponseErr if (now := time()) > getattr(limiter, "_last_updated_at", 0) + 10: current_rate = limiter._rate_per_sec new_rate = current_rate * 0.97 - limiter.time_period /= 0.97 - limiter._rate_per_sec = new_rate - limiter._last_updated_at = now - _logger_info( - "reduced requests per second for %s from %s to %s", - endpoint, - current_rate, - new_rate, - ) + if new_rate >= ENVS.MIN_REQUESTS_PER_SECOND: + limiter.time_period /= 0.97 + limiter._rate_per_sec = new_rate + limiter._last_updated_at = now + _logger_info( + "reduced requests per second for %s from %s to %s", + endpoint, + current_rate, + new_rate, + ) now = time() self._last_rate_limited_at = now