Title: New Mechanism for Avoiding Flood Limits · Issue #3018 · python-telegram-bot/python-telegram-bot · GitHub
Open Graph Title: New Mechanism for Avoiding Flood Limits · Issue #3018 · python-telegram-bot/python-telegram-bot
X Title: New Mechanism for Avoiding Flood Limits · Issue #3018 · python-telegram-bot/python-telegram-bot
Description: supercedes & closes #2139 Below I write down the different thoughts that I had on how we can introduce a new rate limiting mechanism. Pretty much everything is up for discussion. What do you want to have designed differently? What did I ...
Open Graph Description: supercedes & closes #2139 Below I write down the different thoughts that I had on how we can introduce a new rate limiting mechanism. Pretty much everything is up for discussion. What do you want t...
X Description: supercedes & closes #2139 Below I write down the different thoughts that I had on how we can introduce a new rate limiting mechanism. Pretty much everything is up for discussion. What do you wa...
Opengraph URL: https://github.com/python-telegram-bot/python-telegram-bot/issues/3018
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"New Mechanism for Avoiding Flood Limits","articleBody":"supercedes & closes #2139\r\n\r\nBelow I write down the different thoughts that I had on how we can introduce a new rate limiting mechanism. Pretty much everything is up for discussion. What do you want to have designed differently? What did I miss?\r\n\r\n# Feature/Mechanism-Wishlist \r\n\r\n## Integration into PTB\r\n\r\nOne of the major shortcomings of `MessageQueue` was that it was not properly integrated with the framework. IMO that's one of the main points that should be considered. I image that the setup be such that something like\r\n\r\n```python\r\nExtBot(…, rate_limiter=RateLimiter(…))\r\n```\r\nor\r\n```python\r\nApplication.bulder().token('token').rate_limiter(RateLimiter(…)).build()\r\n```\r\nsuffices. That is, `ExtBot` would have a (protected?) attribute holding the rate limiter and all requests would automatically go through that.\r\nNote that I wrote `ExtBot` - this functionality should be purely for `telegram.ext` and not in `telegram`.\r\n\r\n`getUpdates` should not get passed through the rate limiter.\r\n\r\n## Abstract Interface\r\n\r\nI'd like to make this a pluggable interface similar to `BaseRequest`. that way we give users a chance to implement custom, elaborate logic on their own. Such an interface class will probably just have once method - maybe `process_request` or so that `Bot` will call.\r\n\r\nHowever, I'm somewhat unsure what this method should do/return. I see two options:\r\n\r\n1. Return the actual return value of the api request. In this case, `ExtBot` would probably do something like `await self._rate_limiter.process_request(self._post(…))` and in `process_update` one could do somethling like `async with self._semaphore: await coroutine`\r\n2. Alternatively, `procless_update` could return an async context manager, such that `ExtBot` would do `async with self._rate_limiter.process_request(…): await self._post(…)`\r\n\r\nI guess the second one might be a bit harder for python-novices to implement. Then again, doing that is a rather advanced task anyway.\r\nThe first one may also allow for a bit more flexibility in the implementations. OTOH, the context manager approach separates the `Bot` from the `RateLimiter` a bit more clearly, which I potentially like.\r\n\r\n### Open question: Retries\r\n\r\nOne thing that one can think of is whether the rate limiter should be allowed/expected to retry a request in case a `RetryAfter` exception is raised. My gut feeling is that it shouldn't, as it violates the chain of command in the PTB framework. But I'm not completely sure.\r\nIn case we want to allow for retries, we'd have to provide way for that to be possible, e.g. by passing a tuple `(method, kwargs)` such that the rate limiter can call `method(**kwargs)` as often as needed.\r\n\r\n## Per-Request Decisions/Priorities\r\n\r\nIMO it's important to try and allow for requests to be processed differently depending on \r\n\r\n* the endpoint (`answerInlineQuery`, `sendMessage`, etc)\r\n* the parameters passed to that endpoint\r\n* maybe even additional arguments that can be passed on a per-call basis.\r\n\r\nThis would allow to e.g. give answering inline queries a higher priority than sending messages and giving batch-notifications a lower priority than other messages (there even was a Discussions entry requesting something like this at some point, but I can't find it now)\r\n\r\nSo a signature of `process_request` could look like\r\n\r\n```python\r\nasync def process_request(self, (coroutine ,)endpoint: str, data: Dict[str, Any], rate_limit_kwargs: Dict[str, Any]):\r\n ...\r\n``` \r\n\r\nHere IMO the `data` should *not* be the `RequestData` that we pass to `BaseRequest` but basically the plain parameters that are passed to the bot method (after inserting `DefaultValues`.\r\n\r\n# Naming\r\n\r\nMaybe `BaseRateLimiter`?\r\n\r\n# Possible built-in implementation\r\n\r\nWe could provide a basic implementation out of the box. For that we could use a 3rd party dependency like `aiolimiter` or `pyrate-limiter`. Both are published under the MIT license, so that shouldn't be a problem. `aiolimiter` has only one stable release but to me it looks bit bit cleaner, with a small, well defined feature set - though that may just be a first impression.\r\nI imagine that such an implementation could be relatively close to what `MessageQueue` formerly did, i.e. enforce a global delay and a per-group/chat delay. One could think of providing that in a priority-queue style fashion such that user can specify a priority though the `rate_limit_kwargs`.\r\n\r\n# Implementation\r\n\r\nThe main task of calling `process_request` in the right spot should not be too difficult. The main issue that I see is to keep the logic away from `tg.Bot`. For that I have a few ideas:\r\n\r\n## Part 1\r\nFor passing `endpoint` and `data` it should suffice to split up `tg.Bot._post` a bit so that `ExtBot` has a chance to link into the spot between inserting `DefaultValues` and calling `Bot.request._post`.\r\n\r\n## Part 2\r\nFor passing `rate_limit_kwargs`, that's a bit more tricky. We'd have to add a new parameter to all methods of `ExtBot`, but we don't want to add them to `Bot`. \r\n\r\nOne straightforward option would be to split up `Bot.send_message` into `Bot._send_message` and `Bot.send_message`, where only the protected one has the additional parameter. `Bot.send_message` would then just pass everything along to `_send_message` and `ExtBot.send_message` would call `Bot._send_message` directly. This would result in *many* additional loc in `tg.Bot`. Additionally, this doesn't play well with shortcut methods.\r\n\r\nThe other idea that is to piggyback on `api_kwargs`: We add a parameter `rate_limit_kwargs` to `ExtBot.send_message`, but call `super().send_message(…, api_kwargs=api_kwargs |= {secret_key: rate_limit_kwargs)`. Then before passing everything to the rate limiter, we extract them as `rate_limit_kwargs = api_kwargs.pop(secret_key, {})`. This looks like a dirty hack at first glance, but the longer I think about it, the more I actually like it:\r\n\r\n1. `api_kwargs` doesn't interfere with any of the other parameters, so it's rather safe to use for that\r\n2. keeps `Bot` and `ExtBot` apart without introducing endless boilerplate\r\n3. allows the shortcut methods to also have a paremeter `rate_limit_kwargs` - we can just throw an error if they are used with a `Bot` instead of an `ExtBot`\r\n","author":{"url":"https://github.com/Bibo-Joshi","@type":"Person","name":"Bibo-Joshi"},"datePublished":"2022-05-09T19:01:14Z","url":"https://github.com/python-telegram-bot/python-telegram-bot/issues/3018"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:ea1ca3d8-b5af-b6ed-ec18-432210189a44 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E9D2:3A8E64:252871B:30E9D2A:6B025DED |
| html-safe-nonce | 8a2d488a8e802873dd22cf02ce6a5ab68a360e26dce313772ebc03f3c0a704e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFOUQyOjNBOEU2NDoyNTI4NzFCOjMwRTlEMkE6NkIwMjVERUQiLCJ2aXNpdG9yX2lkIjoiODczNDg5NzI2MDUyMzA1MjUyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 7e3dd454ac162a0817e1bd978fadc6d7f2805e776c7db01b7807f03345ffc167 |
| hovercard-subject-tag | issue:1230125532 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python-telegram-bot/python-telegram-bot/3018/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d188ddeb8bf67ab5f96fcf4e0a268924c4b8500d5aee487627031ef67e2e0289/python-telegram-bot/python-telegram-bot/issues/3018 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d188ddeb8bf67ab5f96fcf4e0a268924c4b8500d5aee487627031ef67e2e0289/python-telegram-bot/python-telegram-bot/issues/3018 |
| og:image:alt | supercedes & closes #2139 Below I write down the different thoughts that I had on how we can introduce a new rate limiting mechanism. Pretty much everything is up for discussion. What do you want t... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Bibo-Joshi |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python-telegram-bot/python-telegram-bot git https://github.com/python-telegram-bot/python-telegram-bot.git |
| octolytics-dimension-user_id | 16178365 |
| octolytics-dimension-user_login | python-telegram-bot |
| octolytics-dimension-repository_id | 38696925 |
| octolytics-dimension-repository_nwo | python-telegram-bot/python-telegram-bot |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 38696925 |
| octolytics-dimension-repository_network_root_nwo | python-telegram-bot/python-telegram-bot |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width