-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: port over v3 rate limit code #1
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
Conversation
Traceback |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the webhook adapters and HTTP client to port over the v3 rate limit code. Key changes include:
- Adding a configurable discord_api_url parameter and storing it in both synchronous and asynchronous webhook adapters.
- Refactoring the Route class initializer and merging logic to explicitly accept key parameters and construct URLs.
- Replacing per-bucket lock usage with an executor-based rate limit handling mechanism in the HTTP client.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| discord/webhook/sync.py | Updated WebhookAdapter init and request to use the new discord_api_url via merge. |
| discord/webhook/async_.py | Updated AsyncWebhookAdapter init and request to use the new discord_api_url via merge. |
| discord/http.py | Refactored Route initialization and added the Executor for rate limit handling. |
Signed-off-by: plun1331 <plun1331@gmail.com>
|
@Paillat-dev if you've made sure this works you can merge |
Personally, I've tested that it works with the requests bots make at startup and when responding to interactions/webhooks. I haven't tested it actually getting into a rate limit, so ideally try getting into both a bucket (single route) rate limit and global rate limit if possible. |
Same error when creating text channels |
this is the kind of |
|
Btw I successfully got 24h ratelimited for channel creation: |
Paillat-dev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got the following once when starting, but I'm not sure whether it's related to this pr
ERROR:discord.client:Attempting a reconnect in 1.31s
Traceback (most recent call last):
File "/home/paillat/Documents/pycord-test/discord/client.py", line 645, in connect
self.ws = await asyncio.wait_for(coro, timeout=60.0)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/paillat/.local/share/uv/python/cpython-3.12.9-linux-x86_64-gnu/lib/python3.12/asyncio/tasks.py", line 520, in wait_for
return await fut
^^^^^^^^^
File "/home/paillat/Documents/pycord-test/discord/gateway.py", line 339, in from_client
socket = await client.http.ws_connect(gateway)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/paillat/Documents/pycord-test/discord/http.py", line 249, in ws_connect
return await self.__session.ws_connect(url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/paillat/Documents/pycord-test/.venv/lib/python3.12/site-packages/aiohttp/client.py", line 1021, in _ws_connect
raise WSServerHandshakeError(
aiohttp.client_exceptions.WSServerHandshakeError: 520, message='Invalid response status', url='wss://gateway.discord.gg/?encoding=json&v=10&compress=zlib-stream'
| self._executors: list[Executor] = [] | ||
|
|
||
| user_agent = ( | ||
| "DiscordBot (https://pycord.dev, {0}) Python/{1[0]}.{1[1]} aiohttp/{2}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be cool if we could make it so a user can change this in some way or another
doesn't seem like it should be, @plun1331 what do you think? Seems like some Gateway error |
Paillat-dev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to work for me, I''m just not getting warnings when ratelimited, only debug log messages, idk if that's expected or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the rate limit handling code to port over the v3 rate limit code and update the webhook adapters’ API URL usage.
- Updated the synchronous and asynchronous webhook adapters to accept a customizable API URL and use a new merge method on Route.
- Refactored the Route class and introduced the Executor class in HTTP handling to manage rate limits more dynamically.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| discord/webhook/sync.py | Added discord_api_url parameter and switched to merge for URL. |
| discord/webhook/async_.py | Similar changes as sync.py for asynchronous webhook adapter. |
| discord/http.py | Revised Route initialization, introduced Executor for rate limits, and updated HTTPClient.request logic. |
Comments suppressed due to low confidence (1)
discord/http.py:259
- [nitpick] The variable 'bucket' now holds a merged URL rather than a rate limit bucket identifier. Renaming it to 'merged_url' (or similar) might better reflect its purpose and avoid confusion.
bucket = route.merge(self.api_url)
| or route.guild_id == self.guild_id | ||
| or route.webhook_id == self.webhook_id | ||
| or route.webhook_token == self.webhook_token |
Copilot
AI
May 13, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The equality implementation in Route uses a logical OR to compare individual attributes, which may incorrectly consider two distinct routes as equal if any one attribute matches. Consider using a logical AND for a stricter comparison if full equality is desired.
| or route.guild_id == self.guild_id | |
| or route.webhook_id == self.webhook_id | |
| or route.webhook_token == self.webhook_token | |
| and route.guild_id == self.guild_id | |
| and route.webhook_id == self.webhook_id | |
| and route.webhook_token == self.webhook_token |
Paillat-dev
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, maybe the per route rate limits should also take into account the http method (creating channels is being rate limited but deleting not, however deleting channels is also blocked).
No description provided.