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

fix(torii/graphql): wait to query db after getting broker update #2751

Merged
merged 1 commit into from
Dec 3, 2024

Conversation

lambda-0x
Copy link
Contributor

@lambda-0x lambda-0x commented Dec 3, 2024

since we now use a separate readonly pool we need to wait for sometime for db changes to have been written before reading the db.

fix: #2747

Summary by CodeRabbit

  • Bug Fixes
    • Introduced a delay in event processing to reduce server load during high-frequency event checks.

Copy link

coderabbitai bot commented Dec 3, 2024

Walkthrough

Ohayo, sensei! The changes in this pull request involve a modification to the spawn_rebuilding_graphql_server function in the bin/torii/src/main.rs file. A new else clause has been added to the event processing loop, introducing a one-second delay when the broker is active. This adjustment affects the control flow by slowing down the frequency of event checks while maintaining the overall structure and logic of the function.

Changes

File Change Summary
bin/torii/src/main.rs Modified spawn_rebuilding_graphql_server function to include a one-second delay in the event processing loop when the broker is active.

Sequence Diagram(s)

sequenceDiagram
    participant Broker
    participant Server

    loop Event Processing
        Broker->>Server: Check for new events
        alt Events available
            Server->>Broker: Process event
        else No events
            Server->>Server: Sleep for 1 second
        end
    end
Loading

🪧 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.

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

@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 (1)
bin/torii/src/main.rs (1)

293-294: Ohayo sensei! The fix looks good but could be more configurable.

The addition of a delay after broker updates is a good approach to prevent excessive database queries. However, consider these improvements:

  1. Make the delay duration configurable through a parameter or environment variable
  2. Add a comment explaining why this delay is necessary

Here's a suggested improvement:

+ // Delay between broker updates to prevent excessive database queries
+ const BROKER_UPDATE_DELAY: Duration = Duration::from_secs(1);

 async fn spawn_rebuilding_graphql_server(
     shutdown_tx: Sender<()>,
     pool: Arc<SqlitePool>,
     external_url: Option<Url>,
     proxy_server: Arc<Proxy>,
+    broker_delay: Option<Duration>,
 ) {
     let mut broker = SimpleBroker::<Model>::subscribe();
+    let delay = broker_delay.unwrap_or(BROKER_UPDATE_DELAY);

     loop {
         let shutdown_rx = shutdown_tx.subscribe();
         let (new_addr, new_server) =
             torii_graphql::server::new(shutdown_rx, &pool, external_url.clone()).await;

         tokio::spawn(new_server);

         proxy_server.set_graphql_addr(new_addr).await;

         if broker.next().await.is_none() {
             break;
         } else {
-            tokio::time::sleep(Duration::from_secs(1)).await;
+            tokio::time::sleep(delay).await;
         }
     }
 }
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 938d50f and cc54924.

📒 Files selected for processing (1)
  • bin/torii/src/main.rs (1 hunks)
🔇 Additional comments (1)
bin/torii/src/main.rs (1)

293-294: Verify the impact of broker updates on database performance.

Let's ensure this change effectively addresses the database query rate issue.

Copy link

codecov bot commented Dec 3, 2024

Codecov Report

Attention: Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Project coverage is 56.02%. Comparing base (938d50f) to head (cc54924).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
bin/torii/src/main.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2751      +/-   ##
==========================================
- Coverage   56.03%   56.02%   -0.01%     
==========================================
  Files         427      427              
  Lines       54589    54589              
==========================================
- Hits        30587    30583       -4     
- Misses      24002    24006       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lambda-0x lambda-0x merged commit 859c9d9 into main Dec 3, 2024
12 of 14 checks passed
@lambda-0x lambda-0x deleted the spr/main/b7448a81 branch December 3, 2024 02:02
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.

[BUG] torii might need a restart before fully indexed
2 participants