Skip to content

limit concurrent rpc requests in worker #206

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

Merged
merged 1 commit into from
May 21, 2025

Conversation

iuwqyir
Copy link
Collaborator

@iuwqyir iuwqyir commented May 21, 2025

TL;DR

Implemented a semaphore to limit concurrent RPC requests and improved batch processing timing.

What changed?

  • Added a semaphore mechanism to limit concurrent RPC requests to 20 at a time
  • Moved the batch delay timing from the end of each chunk processing to between initial chunk dispatches
  • Added a debug log message to report successful vs. failed blocks in each chunk
  • Modified the processChunkWithRetry function to accept and use the semaphore
  • Optimized the semaphore acquisition to only cover the actual RPC request, not the entire processing

How to test?

  • Run the application with a large number of blocks to process
  • Verify that RPC requests are properly rate-limited
  • Check logs for the new debug messages showing successful/failed block counts
  • Monitor system performance to ensure the semaphore is preventing resource exhaustion

Why make this change?

The previous implementation could potentially create too many concurrent RPC requests during retry scenarios, which might overwhelm the RPC endpoint or the local system. By implementing a semaphore, we can control the maximum number of concurrent requests regardless of how many chunks are being processed or retried. Additionally, the improved timing of batch delays provides more predictable pacing of initial requests.

Summary by CodeRabbit

  • New Features

    • Added debug logging to display the number of successful and failed blocks after each RPC call.
  • Refactor

    • Improved concurrency control for RPC requests, resulting in more efficient and predictable processing.
    • Adjusted batch processing delay to occur between chunks for smoother operation.

Copy link

coderabbitai bot commented May 21, 2025

Walkthrough

Concurrency control for RPC requests was introduced in the worker by adding a semaphore mechanism. The processChunkWithRetry function now accepts a semaphore channel to limit concurrent RPC calls. The delay between processing chunks was moved from inside the retry function to the main loop in Run. Debug logging was also added.

Changes

File(s) Change Summary
internal/worker/worker.go Updated processChunkWithRetry to accept a semaphore channel for concurrency control, moved batch delay logic to Run, added debug logs, and ensured semaphore is passed during retries. Function signature for processChunkWithRetry was changed.

Sequence Diagram(s)

sequenceDiagram
    participant Run
    participant processChunkWithRetry
    participant RPC

    Run->>processChunkWithRetry: Call with chunk, resultsCh, sem
    processChunkWithRetry->>sem: Acquire semaphore slot
    processChunkWithRetry->>RPC: GetFullBlocks RPC call
    RPC-->>processChunkWithRetry: Return results
    processChunkWithRetry->>sem: Release semaphore slot
    processChunkWithRetry-->>Run: Send results
    Note over Run: Delay (BatchDelay) between chunks after the first
Loading

Possibly related PRs

  • thirdweb-dev/insight#204: Refactored the retry mechanism in processChunkWithRetry, which is directly enhanced in this PR by adding concurrency control and adjusting delay logic.

Suggested reviewers

  • catalyst17
✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need 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)

  • @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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

Copy link
Collaborator Author

iuwqyir commented May 21, 2025

@iuwqyir iuwqyir requested review from catalyst17 and nischitpra May 21, 2025 09:38
@iuwqyir iuwqyir marked this pull request as ready for review May 21, 2025 09:38
@iuwqyir iuwqyir changed the base branch from 05-20-pass_context_to_rpc_and_worker to graphite-base/206 May 21, 2025 09:38
@iuwqyir iuwqyir force-pushed the 05-21-limit_concurrent_rpc_requests_in_worker branch from 18d61f3 to 37f5457 Compare May 21, 2025 09:38
@graphite-app graphite-app bot changed the base branch from graphite-base/206 to main May 21, 2025 09:39
@iuwqyir iuwqyir force-pushed the 05-21-limit_concurrent_rpc_requests_in_worker branch from 37f5457 to aff8413 Compare May 21, 2025 09:39
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

🧹 Nitpick comments (1)
internal/worker/worker.go (1)

104-106: Consider making the semaphore limit configurable.

The semaphore size of 20 is currently hardcoded. Since this is an important tuning parameter that might need adjustment based on environment or load patterns, consider making it configurable through your configuration system.

- // Create a semaphore channel to limit concurrent goroutines
- sem := make(chan struct{}, 20)
+ // Create a semaphore channel to limit concurrent goroutines
+ concurrencyLimit := 20
+ if config.Cfg.RPC.Blocks.MaxConcurrentRequests > 0 {
+     concurrencyLimit = config.Cfg.RPC.Blocks.MaxConcurrentRequests
+ }
+ sem := make(chan struct{}, concurrencyLimit)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b32c54a and aff8413.

📒 Files selected for processing (1)
  • internal/worker/worker.go (6 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
internal/worker/worker.go (2)
internal/rpc/rpc.go (1)
  • GetFullBlockResult (18-22)
configs/config.go (1)
  • Cfg (170-170)
🔇 Additional comments (6)
internal/worker/worker.go (6)

27-27: Function signature updated correctly to support concurrency control.

The addition of the semaphore parameter to processChunkWithRetry is appropriate and consistent with the PR objectives to limit concurrent RPC requests.


34-37: Efficient semaphore usage.

Good implementation of the semaphore acquisition and release. By limiting the semaphore to only the RPC request portion, you're maximizing throughput while still controlling concurrency. The clear comments also help explain the intention.


57-57: Useful debug logging added.

This log message provides valuable information for monitoring and troubleshooting the chunk processing. It will help identify potential issues with specific blocks or RPC endpoints.


70-70: Semaphore correctly passed to recursive calls.

Ensuring the semaphore is passed to recursive calls is crucial for maintaining the concurrency limits.


86-86: Semaphore consistently passed to all recursive paths.

Good attention to detail ensuring all recursive calls include the semaphore, which maintains the concurrency control throughout the entire processing lifecycle.

Also applies to: 91-91


109-112: Improved batch processing timing.

Moving the delay between initial dispatches of chunks rather than after processing each chunk provides more predictable pacing of RPC requests. Skipping the delay for the first chunk makes sense to start processing immediately.

@iuwqyir iuwqyir merged commit 92af676 into main May 21, 2025
6 checks passed
@iuwqyir iuwqyir deleted the 05-21-limit_concurrent_rpc_requests_in_worker branch May 21, 2025 09:53
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