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

(DAL) syncmap implementation #1973

Merged
merged 3 commits into from
Aug 1, 2024
Merged

Conversation

nick-bisonai
Copy link
Collaborator

Description

  • Syncmap instead of map + mutex
  • Implement threadsafe websocket client for better WriteJson performance

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist before requesting a review

  • I have performed a self-review of my code.
  • If it is a core feature, I have added thorough tests.

Deployment

  • Should publish npm package
  • Should publish Docker image

@nick-bisonai nick-bisonai self-assigned this Aug 1, 2024
@nick-bisonai nick-bisonai requested a review from a team as a code owner August 1, 2024 02:27
Copy link
Contributor

coderabbitai bot commented Aug 1, 2024

Warning

Rate limit exceeded

@nick-bisonai has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 12 minutes and 44 seconds before requesting another review.

How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

Commits

Files that changed from the base of the PR and between 8cd4cf2 and 7da1ff6.

Walkthrough

Walkthrough

The updates introduce a ThreadSafeClient to manage WebSocket connections in a thread-safe manner, enhancing concurrency safety across various components. Key improvements include refined handling of WebSocket subscriptions and client management within the Hub. The changes collectively improve the robustness of WebSocket interactions, promoting safer concurrent access and simplifying connection management. Additionally, a new method for bulk insertion of WebSocket subscriptions into the database enhances performance.

Changes

Files Summary of Changes
node/pkg/dal/api/client.go, node/pkg/dal/api/controller.go, node/pkg/dal/api/hub.go, node/pkg/dal/api/types.go Introduced a ThreadSafeClient struct for managing WebSocket connections safely. Updated associated methods and structures for improved concurrency and error handling.
node/pkg/dal/utils/stats/stats.go Added InsertWebsocketSubscriptions function for bulk insertion of subscriptions into the database, enhancing performance and efficiency.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant ThreadSafeClient
    participant Hub
    participant Database

    Client->>ThreadSafeClient: Connect
    ThreadSafeClient->>Hub: Register client
    Hub->>ThreadSafeClient: Acknowledge registration
    Client->>ThreadSafeClient: Send subscription requests
    ThreadSafeClient->>Hub: Forward subscription requests
    Hub->>Database: Insert subscriptions in bulk
    Database-->>Hub: Confirm insertions
    Hub-->>ThreadSafeClient: Acknowledge subscriptions
    ThreadSafeClient-->>Client: Confirm subscriptions
Loading

Poem

🐰 In the meadow bright and fair,
A client hops without a care!
With threads so safe, they dance and play,
Subscriptions bloom, come what may.
Oh, rejoice, dear friends, in code we trust,
For WebSocket magic is a must! 🌼


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

Copy link
Contributor

@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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 85b145c and 5b5d826.

Files selected for processing (5)
  • node/pkg/dal/api/client.go (1 hunks)
  • node/pkg/dal/api/controller.go (4 hunks)
  • node/pkg/dal/api/hub.go (4 hunks)
  • node/pkg/dal/api/types.go (2 hunks)
  • node/pkg/dal/utils/stats/stats.go (1 hunks)
Additional context used
Learnings (1)
node/pkg/dal/api/controller.go (1)
Learnt from: nick-bisonai
PR: Bisonai/orakl#1791
File: node/pkg/dal/api/controller.go:0-0
Timestamp: 2024-07-12T02:42:57.258Z
Learning: In the `node/pkg/dal/api/controller.go`, the `handleWebsocket` function no longer requires explicit validation for the `X-Api-Key` header, as this is handled by the `gofiber keyauth` middleware in `initializer.go`.
Additional comments not posted (13)
node/pkg/dal/api/types.go (1)

17-19: Enhance thread safety with sync.Map and ThreadSafeClient.

The use of sync.Map for clients and ThreadSafeClient for register and unregister channels improves thread safety and efficiency. Ensure that all interactions with clients are thread-safe and consistent.

node/pkg/dal/api/client.go (5)

16-20: Constructor for ThreadSafeClient is correct.

The constructor correctly initializes the ThreadSafeClient structure with the provided WebSocket connection.


22-30: Thread-safe WriteJSON method is correctly implemented.

The method correctly locks the mutex before writing JSON data to ensure thread safety. Error handling is also appropriately managed.


34-40: ReadJSON method is correctly implemented with appropriate comments.

The method reads JSON data without locking the mutex, as expected. The comments clarify the expected usage, ensuring it is not called concurrently.


42-44: Close method is correctly implemented.

The method correctly closes the WebSocket connection.


46-48: WriteControl method is correctly implemented.

The method correctly writes control messages to the WebSocket connection.

node/pkg/dal/utils/stats/stats.go (1)

74-81: InsertWebsocketSubscriptions method is correctly implemented.

The function correctly constructs the entries and performs the bulk insertion into the database, improving performance for multiple subscriptions.

node/pkg/dal/api/hub.go (5)

29-30: LGTM!

The channels for client registration and unregistration now operate with *ThreadSafeClient, enhancing thread safety.


48-51: LGTM!

The function now handles *ThreadSafeClient for registration and unregistration, aligning with the new client type.


56-57: LGTM!

The use of LoadOrStore simplifies the client addition process while ensuring thread safety.


60-82: LGTM!

The use of LoadAndDelete simplifies the client removal process while ensuring thread safety. Error handling for closing the connection is appropriate.


109-129: LGTM!

The use of sync.WaitGroup enhances the efficiency of broadcasting messages by managing concurrent writes.

node/pkg/dal/api/controller.go (1)

Line range hint 24-84:
LGTM!

The changes enhance thread safety by wrapping the original WebSocket connection in threadSafeClient. The registration and unregistration processes, as well as subscription management, are now thread-safe.

Copy link
Contributor

@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: 1

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 5b5d826 and 8cd4cf2.

Files selected for processing (2)
  • node/pkg/dal/tests/stats_test.go (1 hunks)
  • node/pkg/dal/utils/stats/stats.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • node/pkg/dal/utils/stats/stats.go
Additional comments not posted (1)
node/pkg/dal/tests/stats_test.go (1)

99-99: Verify the function usage in the codebase.

Ensure that all function calls to stats.InsertWebsocketSubscriptions properly handle multiple topics.

Verification successful

Verify the handling of multiple topics in controller.go and suggest adding tests for multiple topics in stats_test.go.

  • Ensure that the function InsertWebsocketSubscriptions in controller.go handles multiple topics properly.
  • Suggest adding tests for multiple topics in stats_test.go.

Verify the function definition InsertWebsocketSubscriptions in the entire codebase.

Ensure that the function InsertWebsocketSubscriptions is properly implemented and handles multiple topics.


Suggest adding tests for multiple topics in stats_test.go.

The function InsertWebsocketSubscriptions correctly handles multiple topics by iterating over them and creating entries for each one. However, the test in stats_test.go only tests with a single topic. It is recommended to add tests for multiple topics to ensure comprehensive coverage.

  • File: node/pkg/dal/tests/stats_test.go
  • Lines: 99-99
  • Function: TestWebsocketSubcription

Suggested Action:
Add tests for multiple topics in TestWebsocketSubcription to ensure that InsertWebsocketSubscriptions handles multiple topics correctly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `stats.InsertWebsocketSubscriptions` handle multiple topics.

# Test: Search for the function usage. Expect: Proper handling of multiple topics.
rg --type go -A 5 $'stats.InsertWebsocketSubscriptions'

Length of output: 871


Script:

#!/bin/bash
# Description: Verify the handling of multiple topics in `controller.go`.

# Test: Search for the function implementation. Expect: Proper handling of multiple topics.
rg --type go -A 10 $'func InsertWebsocketSubscriptions' node/pkg/dal/api/controller.go

Length of output: 87


Script:

#!/bin/bash
# Description: Verify the function definition `InsertWebsocketSubscriptions` in the entire codebase.

# Test: Search for the function definition. Expect: Proper handling of multiple topics.
rg --type go -A 10 $'func InsertWebsocketSubscriptions'

Length of output: 810

node/pkg/dal/tests/stats_test.go Show resolved Hide resolved
@nick-bisonai nick-bisonai force-pushed the feat/syncmap-implementation branch from 8cd4cf2 to 7da1ff6 Compare August 1, 2024 03:44
@nick-bisonai nick-bisonai merged commit fcbfdcd into master Aug 1, 2024
2 checks passed
@nick-bisonai nick-bisonai deleted the feat/syncmap-implementation branch August 1, 2024 05:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant