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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions services/rfq/relayer/service/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
return nil
}

defer unlocker.Unlock()
shouldUnlock := true
defer func() {
if shouldUnlock {
unlocker.Unlock()
}

Check warning on line 54 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L50-L54

Added lines #L50 - L54 were not covered by tests
}()

_, err = r.db.GetQuoteRequestByID(ctx, req.TransactionId)
// expect no results
Expand Down Expand Up @@ -120,12 +125,17 @@
if err != nil {
return fmt.Errorf("could not get quote request handler: %w", err)
}
// Forward instead of lock since we called lock above.
fwdErr := qr.Forward(ctx, dbReq)
if fwdErr != nil {
logger.Errorf("could not forward to handle seen: %w", fwdErr)
span.AddEvent("could not forward to handle seen")
}

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

span.AddEvent(fmt.Sprintf("could not forward to handle seen: %s", fwdErr.Error()))
}

Check warning on line 137 in services/rfq/relayer/service/handlers.go

View check run for this annotation

Codecov / codecov/patch

services/rfq/relayer/service/handlers.go#L130-L137

Added lines #L130 - L137 were not covered by tests
}()
Comment on lines +128 to +138
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()))
	}
}()


return nil
}
Expand Down
Loading