Skip to content
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

[ECO-5054] Extract common code for message processing in RealtimeChannel to a separate function #1911

Merged
merged 1 commit into from
Nov 29, 2024

Conversation

VeskeR
Copy link
Contributor

@VeskeR VeskeR commented Oct 25, 2024

Resolves #1907

It slightly increases the bundle size for a default bundle on main branch (0.12KiB for All exports modular plugin, 0.04KiB if gzipped), but leads to a decrease as we add new message processing (see #1910 with STATE and STATE_SYNC messages).

This also fixes the issue with processing Presence messages, where an error thrown from a decoding call would prevent us from setting connectionId, timestamp and id fields for a message. Now this behavior is aligned with how it worked with regular messages.

Summary by CodeRabbit

  • New Features

    • Enhanced message processing logic for improved handling of presence messages.
    • Introduced structured error handling for message decoding.
  • Bug Fixes

    • Improved error recovery process for decoding errors, ensuring better channel state management.

@VeskeR VeskeR requested a review from owenpearson October 25, 2024 10:28
Copy link

coderabbitai bot commented Oct 25, 2024

Walkthrough

The changes in this pull request focus on enhancing the message processing logic within the RealtimeChannel class in src/common/lib/client/realtimechannel.ts. Key modifications include the renaming of the presence variable to presenceMessages, the introduction of a new private method _decodeAndPrepareMessages for handling message decoding, and improved error handling mechanisms. The processMessage method has been streamlined to utilize this new method for both presence and regular messages, leading to better structure and clarity in the code.

Changes

File Path Change Summary
src/common/lib/client/realtimechannel.ts - Renamed presence to presenceMessages in processMessage method.
- Added private method _decodeAndPrepareMessages for message processing.
- Improved error handling during message decoding.
- Adjusted logging statements for clarity.

Assessment against linked issues

Objective Addressed Explanation
Extract common code between presence and regular message processing (#1907, ECO-5054)

Possibly related PRs

Suggested reviewers

  • ttypic

Poem

In the channel where messages flow,
A rabbit hops, with skills to show.
With presence clear and errors tamed,
Our code is neat, and well-framed!
Let's decode with joy, and cheer,
For every message, loud and clear! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@VeskeR VeskeR force-pushed the 1907/extract-common-message-decoding branch from c1681b2 to ca0d08d Compare October 25, 2024 10:28
@github-actions github-actions bot temporarily deployed to staging/pull/1911/bundle-report October 25, 2024 10:29 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1911/typedoc October 25, 2024 10:29 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1911/features October 25, 2024 10:29 Inactive
@VeskeR VeskeR force-pushed the 1907/extract-common-message-decoding branch from ca0d08d to 7b9cfa9 Compare October 25, 2024 10:35
@VeskeR VeskeR requested a review from ttypic October 25, 2024 10:35
@github-actions github-actions bot temporarily deployed to staging/pull/1911/bundle-report October 25, 2024 10:35 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1911/typedoc October 25, 2024 10:35 Inactive
@github-actions github-actions bot temporarily deployed to staging/pull/1911/features October 25, 2024 10:35 Inactive
@VeskeR VeskeR changed the title Extract common code for message processing in RealtimeChannel to a separate function [ECO-5054] Extract common code for message processing in RealtimeChannel to a separate function Oct 25, 2024
@VeskeR VeskeR force-pushed the 1907/extract-common-message-decoding branch from 7b9cfa9 to d17f97a Compare November 8, 2024 16:35
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
src/common/lib/client/realtimechannel.ts (2)

696-727: Consider decoding messages in parallel for improved performance

In the _decodeAndPrepareMessages method, messages are decoded sequentially within a for loop using await decodeFn(msg). If the decoding of each message is independent, consider decoding all messages in parallel using Promise.all, which can enhance performance when processing a large array of messages.

Here is how you might refactor the code:

-        for (let i = 0; i < messages.length; i++) {
-          const msg = messages[i];
-          try {
-            await decodeFn(msg);
-          } catch (e) {
-            Logger.logAction(
-              this.logger,
-              Logger.LOG_ERROR,
-              'RealtimeChannel.decodeAndPrepareMessages()',
-              (e as Error).toString(),
-            );
-            if (decodeErrorRecoveryHandler) {
-              const { unrecoverableError } = decodeErrorRecoveryHandler(e as Error);
-              if (unrecoverableError) {
-                // break out of for loop by returning
-                return { unrecoverableError: true };
-              }
-            }
-          }
-          if (!msg.connectionId) msg.connectionId = connectionId;
-          if (!msg.timestamp) msg.timestamp = timestamp;
-          if (!msg.id) msg.id = id + ':' + i;
-        }
+        const decodePromises = messages.map(async (msg, index) => {
+          try {
+            await decodeFn(msg);
+          } catch (e) {
+            Logger.logAction(
+              this.logger,
+              Logger.LOG_ERROR,
+              'RealtimeChannel._decodeAndPrepareMessages()',
+              `Error decoding message at index ${index}: ${(e as Error).toString()}`,
+            );
+            if (decodeErrorRecoveryHandler) {
+              const { unrecoverableError } = decodeErrorRecoveryHandler(e as Error);
+              if (unrecoverableError) {
+                // Throw to exit early
+                throw e;
+              }
+            }
+          }
+          if (!msg.connectionId) msg.connectionId = connectionId;
+          if (!msg.timestamp) msg.timestamp = timestamp;
+          if (!msg.id) msg.id = id + ':' + index;
+        });
+        try {
+          await Promise.all(decodePromises);
+          return { unrecoverableError: false };
+        } catch (e) {
+          return { unrecoverableError: true };
+        }

705-710: Enhance error logging with additional context

When logging errors in _decodeAndPrepareMessages, consider including more context, such as the message index or contents, to facilitate debugging and error tracing.

Apply this diff to improve the log statement:

             Logger.logAction(
               this.logger,
               Logger.LOG_ERROR,
               'RealtimeChannel.decodeAndPrepareMessages()',
-              (e as Error).toString(),
+              `Error decoding message at index ${i}: ${(e as Error).toString()}`,
             );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 18a2559 and d17f97a.

📒 Files selected for processing (2)
  • scripts/moduleReport.ts (1 hunks)
  • src/common/lib/client/realtimechannel.ts (4 hunks)
🔇 Additional comments (2)
scripts/moduleReport.ts (1)

9-9: LGTM! Consider adding a comment explaining the threshold increase.

The increase in the raw bundle size threshold from 98 to 99 KiB aligns with the expected 0.12 KiB increase mentioned in the PR objectives. This temporary increase is justified as it's expected to lead to a decrease when new message processing is added (PR #1910).

Consider adding a comment explaining why this threshold was increased to help future maintainers understand the context.

Add a comment above the constant:

 // The maximum size we allow for a minimal useful Realtime bundle (i.e. one that can subscribe to a channel)
+// Note: Threshold increased to 99 KiB to accommodate the temporary size increase from extracting common message processing code.
+// This is expected to decrease when new message processing is added (see PR #1910).
 const minimalUsefulRealtimeBundleSizeThresholdsKiB = { raw: 99, gzip: 30 };

Let's verify that the new threshold accommodates the actual bundle size:

src/common/lib/client/realtimechannel.ts (1)

684-727: LGTM: Well-structured implementation of _decodeAndPrepareMessages

The new _decodeAndPrepareMessages method is well-implemented with clear logic and proper error handling. This refactoring improves code reuse and readability in the message processing flow.

Copy link
Collaborator

@ttypic ttypic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

src/common/lib/client/realtimechannel.ts Show resolved Hide resolved
Copy link
Member

@owenpearson owenpearson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good!

@owenpearson owenpearson self-requested a review November 22, 2024 14:30
…parate function

Also fixes the issue with processing Presence messages, where a decoding
error would prevent us from setting `connectionId`, `timestamp` and `id`
fields for a message. Now this behavior is aligned with how it worked
with regular messages.

Resolves #1907
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (2)
src/common/lib/client/realtimechannel.ts (2)

684-727: Good extraction of common logic, with room for minor improvements.

The new method effectively consolidates common message processing logic. Consider these improvements:

  1. Make the error logging more specific by including the message type and index:
-          'RealtimeChannel.decodeAndPrepareMessages()',
+          'RealtimeChannel._decodeAndPrepareMessages()',
-          (e as Error).toString(),
+          `Failed to decode ${msg.constructor.name} at index ${i}: ${(e as Error).toString()}`,
  1. Consider splitting the method into smaller, focused functions:
    • Message decoding with error handling
    • Message field population (connectionId, timestamp, id)

This would improve maintainability and make the code easier to test.


Line range hint 572-727: Well-structured extraction of common message processing logic.

The refactoring successfully:

  1. Reduces code duplication between presence and regular message processing
  2. Provides a consistent approach to message decoding and error handling
  3. Makes the code more maintainable for future message types (like STATE and STATE_SYNC mentioned in PR objectives)

The slight increase in bundle size is a reasonable trade-off for the improved code structure and maintainability.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between d17f97a and 282b188.

📒 Files selected for processing (1)
  • src/common/lib/client/realtimechannel.ts (4 hunks)
🔇 Additional comments (2)
src/common/lib/client/realtimechannel.ts (2)

572-583: LGTM! Clean refactoring of presence message processing.

The variable rename from 'presence' to 'presenceMessages' improves clarity, and the extraction of common decoding logic reduces code duplication while maintaining the same functionality.


624-652: LGTM! Improved error handling and code organization.

The changes effectively:

  • Centralize error handling logic in the new method
  • Add clear early return pattern for unrecoverable errors
  • Make error handling consistent between presence and regular messages

@VeskeR VeskeR merged commit 416c9b0 into main Nov 29, 2024
14 checks passed
@VeskeR VeskeR deleted the 1907/extract-common-message-decoding branch November 29, 2024 05:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Extract common code between presence and regular message processing in RealtimeChannel
3 participants