Skip to content

Commit f2830cb

Browse files
committed
feat: Implemented HTTP 50x error tracking, per-route exhaust ratelimiting, refactored decor.py
1 parent d6f28b7 commit f2830cb

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

interactions/api/http/request.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ async def request(self, route: Route, **kwargs) -> Optional[Any]:
9898
"""
9999

100100
kwargs["headers"] = {**self._headers, **kwargs.get("headers", {})}
101-
kwargs["headers"]["Content-Type"] = "application/json"
101+
if kwargs.get("json"):
102+
kwargs["headers"]["Content-Type"] = "application/json"
102103

103104
reason = kwargs.pop("reason", None)
104105
if reason:
@@ -165,7 +166,7 @@ async def request(self, route: Route, **kwargs) -> Optional[Any]:
165166
# This "redundant" debug line is for debug use and tracing back the error codes.
166167

167168
raise HTTPException(data["code"], message=data["message"])
168-
elif remaining and not int(remaining):
169+
if remaining and not int(remaining):
169170
if response.status == 429:
170171
log.warning(
171172
f"The HTTP client has encountered a per-route ratelimit. Locking down future requests for {reset_after} seconds."
@@ -181,6 +182,18 @@ async def request(self, route: Route, **kwargs) -> Optional[Any]:
181182
self._loop.call_later(
182183
self._global_lock.reset_after, self._global_lock.lock.release
183184
)
185+
elif int(remaining) == 0:
186+
log.warning(
187+
f"The HTTP client has exhausted a per-route ratelimit. Locking route for {reset_after} seconds."
188+
)
189+
self._loop.call_later(reset_after, _limiter.release_lock())
190+
191+
if response.status in {500, 502, 504}:
192+
log.warning(
193+
f"{route.endpoint} Received {response.status}... retrying in {1 + tries * 2} seconds"
194+
)
195+
await asyncio.sleep(1 + tries * 2)
196+
continue
184197

185198
log.debug(f"RETURN {response.status}: {dumps(data, indent=4, sort_keys=True)}")
186199

interactions/decor.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ def command(
3434
_description: str = "" if description is MISSING else description
3535
_options: list = []
3636

37-
_name_localizations: dict = {} if name_localizations is MISSING else name_localizations
38-
_description_localizations: dict = (
39-
{} if description_localizations is MISSING else description_localizations
37+
_name_localizations = (
38+
{}
39+
if name_localizations is MISSING
40+
else {k.value if isinstance(k, Locale) else k: v for k, v in name_localizations.items()}
41+
)
42+
_description_localizations = (
43+
{}
44+
if description_localizations is MISSING
45+
else {
46+
k.value if isinstance(k, Locale) else k: v for k, v in description_localizations.items()
47+
}
4048
)
41-
42-
_name_localizations = {
43-
k.value if isinstance(k, Locale) else k: v for k, v in _name_localizations.items()
44-
}
45-
_description_localizations = {
46-
k.value if isinstance(k, Locale) else k: v for k, v in _description_localizations.items()
47-
}
4849

4950
if options is not MISSING:
5051
if all(isinstance(option, Option) for option in options):

0 commit comments

Comments
 (0)