Increase Telegram request timeouts (connect, pool, write) to fix TimedOut errors#164
Increase Telegram request timeouts (connect, pool, write) to fix TimedOut errors#164ovchynnikov merged 3 commits intoovchynnikov:mainfrom
Conversation
- Added TELEGRAM_CONNECT_TIMEOUT=30 and TELEGRAM_POOL_TIMEOUT=10 - Set default timeouts for all requests via HTTPXRequest(connect_timeout, pool_timeout, read_timeout, write_timeout) - Explicitly passed connect_timeout to send_video / send_photo / send_media_group calls for consistency - Prevents TimedOut / ConnectTimeout errors during LLM replies and rate-limit messages
WalkthroughRefactors Telegram API request handling in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip CodeRabbit can use your project's `ruff` configuration to improve the quality of Python code reviews.Add a Ruff configuration file to your project to customize how CodeRabbit runs |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
src/main.py (2)
414-417: Redundant timeout specification.Since
HTTPXRequestis now configured with these timeout defaults (lines 957-962), explicitly passing them on each API call is redundant—per-call values override the defaults. Consider removing the per-call timeouts to centralize configuration, or remove theHTTPXRequesttimeouts if per-call control is intentional.♻️ Option A: Remove per-call timeouts (rely on HTTPXRequest defaults)
await update.message.chat.send_video( video=video_file, width=width, height=height, has_spoiler=has_spoiler, disable_notification=True, - connect_timeout=TELEGRAM_CONNECT_TIMEOUT, - write_timeout=TELEGRAM_WRITE_TIMEOUT, - read_timeout=TELEGRAM_READ_TIMEOUT, reply_to_message_id=update.message.message_id, )🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main.py` around lines 414 - 417, Remove the redundant per-call timeout arguments (connect_timeout, write_timeout, read_timeout) from the API call where reply_to_message_id=update.message.message_id is passed so the call uses the centralized HTTPXRequest defaults configured in HTTPXRequest (lines ~957-962); locate the call site in src/main.py that includes those timeout kwargs and simply delete those three parameters (leave reply_to_message_id intact).
51-54: Discrepancy with PR description: env vars vs hardcoded constants.The PR description states these should be environment variables (
TELEGRAM_CONNECT_TIMEOUT=30andTELEGRAM_POOL_TIMEOUT=10), but the implementation uses hardcoded constants with different values (60 and 30). Consider making these configurable viaos.getenv()for runtime flexibility, consistent with other settings in this file.♻️ Proposed fix to use environment variables
-TELEGRAM_CONNECT_TIMEOUT = 60 -TELEGRAM_POOL_TIMEOUT = 30 -TELEGRAM_READ_TIMEOUT = 120 -TELEGRAM_WRITE_TIMEOUT = 120 +TELEGRAM_CONNECT_TIMEOUT = int(os.getenv("TELEGRAM_CONNECT_TIMEOUT", "60")) +TELEGRAM_POOL_TIMEOUT = int(os.getenv("TELEGRAM_POOL_TIMEOUT", "30")) +TELEGRAM_READ_TIMEOUT = int(os.getenv("TELEGRAM_READ_TIMEOUT", "120")) +TELEGRAM_WRITE_TIMEOUT = int(os.getenv("TELEGRAM_WRITE_TIMEOUT", "120"))🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/main.py` around lines 51 - 54, The hardcoded timeout constants TELEGRAM_CONNECT_TIMEOUT and TELEGRAM_POOL_TIMEOUT conflict with the PR description and should be made configurable via environment variables; update their assignments (and ideally TELEGRAM_READ_TIMEOUT and TELEGRAM_WRITE_TIMEOUT for consistency) to use os.getenv with integer conversion and sane defaults (TELEGRAM_CONNECT_TIMEOUT default 30, TELEGRAM_POOL_TIMEOUT default 10, keep or set defaults for READ/WRITE as before) while validating/converting the env values to ints before assigning to the constants used throughout the module.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/main.py`:
- Around line 414-417: Remove the redundant per-call timeout arguments
(connect_timeout, write_timeout, read_timeout) from the API call where
reply_to_message_id=update.message.message_id is passed so the call uses the
centralized HTTPXRequest defaults configured in HTTPXRequest (lines ~957-962);
locate the call site in src/main.py that includes those timeout kwargs and
simply delete those three parameters (leave reply_to_message_id intact).
- Around line 51-54: The hardcoded timeout constants TELEGRAM_CONNECT_TIMEOUT
and TELEGRAM_POOL_TIMEOUT conflict with the PR description and should be made
configurable via environment variables; update their assignments (and ideally
TELEGRAM_READ_TIMEOUT and TELEGRAM_WRITE_TIMEOUT for consistency) to use
os.getenv with integer conversion and sane defaults (TELEGRAM_CONNECT_TIMEOUT
default 30, TELEGRAM_POOL_TIMEOUT default 10, keep or set defaults for
READ/WRITE as before) while validating/converting the env values to ints before
assigning to the constants used throughout the module.
Summary by CodeRabbit