-
Notifications
You must be signed in to change notification settings - Fork 191
feat: implement an exponential-backoff to find echo message #747
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
base: main
Are you sure you want to change the base?
Conversation
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 1
🧹 Nitpick comments (2)
api/src/chat/services/chat.service.ts (2)
226-227: Consider renaming the variable for clarity.The variable
messageis misleading sincefindMessageWithRetriesreturns a boolean, not a message object. Consider renaming it tomessageExistsorfoundfor better code readability.- const message = await this.findMessageWithRetries(mid); + const messageExists = await this.findMessageWithRetries(mid); - if (!message) { + if (!messageExists) {Also applies to: 230-240
264-274: Consider adding bounds to prevent excessive delays.While the exponential backoff implementation is correct, consider adding an upper bound to the delay to prevent excessively long waits in edge cases. With the current implementation, the maximum delay could reach 1.6 seconds (100ms * 2^4) which seems reasonable, but adding a max delay parameter would make the method more robust.
async findMessageWithRetries( mid: string, maxRetries = 5, initialDelay = 100, + maxDelay = 1000, ) { let attempt = 0; let delay = initialDelay; while (attempt < maxRetries) { const exists = await this.messageService.count({ mid }); if (exists > 0) { return true; // Message exists } attempt++; await new Promise((resolve) => setTimeout(resolve, delay)); - delay *= 2; // Exponential backoff + delay = Math.min(delay * 2, maxDelay); // Exponential backoff with cap }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
api/src/chat/services/chat.service.ts(1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
api/src/chat/services/chat.service.ts
[error] 228-228: This is an unexpected use of the debugger statement.
Unsafe fix: Remove debugger statement
(lint/suspicious/noDebugger)
🔇 Additional comments (1)
api/src/chat/services/chat.service.ts (1)
248-280: Well-implemented retry mechanism with exponential backoff.The implementation correctly addresses the timing issue described in the PR objectives. The exponential backoff strategy is appropriate for this use case, and the default parameters (5 retries, 100ms initial delay) provide a good balance between reliability and performance.
Key strengths:
- Uses
count()for efficient existence checking- Implements proper exponential backoff (doubling delay)
- Includes appropriate logging for debugging
- Has reasonable default parameters
- Clear JSDoc documentation
| this.eventEmitter.emit('hook:chatbot:sent', sentMessage); | ||
| const mid = event.getId(); | ||
| const message = await this.findMessageWithRetries(mid); | ||
| debugger; |
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.
Remove the debugger statement.
The debugger statement should be removed before merging to production as it will cause the code to break in debugger mode unexpectedly.
- debugger;📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| debugger; |
🧰 Tools
🪛 Biome (1.9.4)
[error] 228-228: This is an unexpected use of the debugger statement.
Unsafe fix: Remove debugger statement
(lint/suspicious/noDebugger)
🤖 Prompt for AI Agents
In api/src/chat/services/chat.service.ts at line 228, remove the debugger
statement to prevent the code from breaking unexpectedly in debugger mode when
running in production.
This PR introduces exponential backoff when checking for existing messages to prevent duplicate echo messages. Previously, echo messages could arrive before the original message was saved, causing them to be incorrectly treated as new messages.
Changes:
Behavior:
serves Issue #746
Summary by CodeRabbit