Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: MIN_REQUESTS_PER_SECOND env var #345

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dank_mids/ENVIRONMENT_VARIABLES.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 10 additions & 9 deletions dank_mids/helpers/_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading