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

feat(rfq-relayer): forward bridge requests in new goroutine #3276

Merged
merged 2 commits into from
Oct 11, 2024

Conversation

dwasse
Copy link
Collaborator

@dwasse dwasse commented Oct 11, 2024

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of bridge requests to prevent potential race conditions.
    • Enhanced error handling and logging for better traceability during execution.
  • Refactor

    • Updated the flow of lock management to ensure it remains in place during asynchronous operations.
    • Minor adjustments made to comments and code structure for clarity.

Copy link
Contributor

coderabbitai bot commented Oct 11, 2024

Walkthrough

The changes in this pull request primarily focus on the handleBridgeRequestedLog method within the Relayer struct in services/rfq/relayer/service/handlers.go. The lock management during bridge request processing has been modified to prevent race conditions by introducing a boolean flag for controlling the mutex unlock timing. Enhancements to error handling and logging have been implemented, improving traceability. Minor adjustments to comments and code structure were made, but the core logic remains unchanged.

Changes

File Path Change Summary
services/rfq/relayer/service/handlers.go Modified handleBridgeRequestedLog method to improve lock management and introduced a shouldUnlock flag. Enhanced error handling and logging. Minor adjustments to comments and structure. Method signatures updated but logic remains unchanged.

Poem

In the meadow where bunnies hop,
Locks and logs take a little stop.
With a flag to guide the way,
No more race in the fray!
Hooray for changes, bright and new,
A smoother path for me and you! 🐇✨


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.

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.

@github-actions github-actions bot added go Pull requests that update Go code size/xs labels Oct 11, 2024
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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between bedbf2d and 42893f0.

📒 Files selected for processing (1)
  • services/rfq/relayer/service/handlers.go (2 hunks)
🧰 Additional context used
🔇 Additional comments (1)
services/rfq/relayer/service/handlers.go (1)

50-55: Properly manage lock unlocking with 'shouldUnlock' variable

The introduction of the shouldUnlock variable along with the deferred function ensures that the lock is only released when appropriate. This prevents premature unlocking and potential race conditions.

Comment on lines +128 to +138

// Forward in new goroutine and retain the lock.
shouldUnlock = false
go func() {
defer unlocker.Unlock()
fwdErr := qr.Forward(ctx, dbReq)
if fwdErr != nil {
logger.Errorf("could not forward to handle seen: %w", fwdErr)
span.AddEvent(fmt.Sprintf("could not forward to handle seen: %s", fwdErr.Error()))
}
}()
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid holding locks during asynchronous operations

Holding the lock while executing qr.Forward in a new goroutine can lead to performance issues or deadlocks if the forwarded operation takes time or encounters errors. It's generally best to minimize the duration of held locks and avoid keeping them during long-running asynchronous tasks.

Consider releasing the lock before starting the goroutine or restructuring the code so that the lock is not held during the asynchronous operation. Here's a possible refactoring:

// Release the lock before forwarding
unlocker.Unlock()
go func() {
	fwdErr := qr.Forward(ctx, dbReq)
	if fwdErr != nil {
		logger.Errorf("could not forward to handle seen: %v", fwdErr)
		span.AddEvent(fmt.Sprintf("could not forward to handle seen: %s", fwdErr.Error()))
	}
}()

defer unlocker.Unlock()
fwdErr := qr.Forward(ctx, dbReq)
if fwdErr != nil {
logger.Errorf("could not forward to handle seen: %w", fwdErr)
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct format specifier in logger message

The format specifier %w is used with fmt.Errorf for error wrapping and may not be supported by the logger, resulting in incorrect error messages. Instead, use %v or %s to properly format the error.

Apply this diff to fix the format specifier:

-logger.Errorf("could not forward to handle seen: %w", fwdErr)
+logger.Errorf("could not forward to handle seen: %v", fwdErr)
📝 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.

Suggested change
logger.Errorf("could not forward to handle seen: %w", fwdErr)
logger.Errorf("could not forward to handle seen: %v", fwdErr)

Copy link

codecov bot commented Oct 11, 2024

Codecov Report

Attention: Patch coverage is 0% with 13 lines in your changes missing coverage. Please review.

Please upload report for BASE (master@e74153c). Learn more about missing BASE report.
Report is 49 commits behind head on master.

Files with missing lines Patch % Lines
services/rfq/relayer/service/handlers.go 0.00000% 13 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master       #3276   +/-   ##
============================================
  Coverage          ?   24.99618%           
============================================
  Files             ?         198           
  Lines             ?       13074           
  Branches          ?          82           
============================================
  Hits              ?        3268           
  Misses            ?        9526           
  Partials          ?         280           
Flag Coverage Δ
opbot 0.48870% <ø> (?)
promexporter 6.81642% <ø> (?)
rfq 24.61353% <0.00000%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

Copy link

Deploying sanguine-fe with  Cloudflare Pages  Cloudflare Pages

Latest commit: 42893f0
Status: ✅  Deploy successful!
Preview URL: https://29c25840.sanguine-fe.pages.dev
Branch Preview URL: https://feat-relayer-fwd.sanguine-fe.pages.dev

View logs

@dwasse dwasse merged commit 099624f into master Oct 11, 2024
42 checks passed
@dwasse dwasse deleted the feat/relayer-fwd branch October 11, 2024 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go Pull requests that update Go code needs-go-generate-services/rfq size/xs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant