-
Notifications
You must be signed in to change notification settings - Fork 33
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
RFQ Quoter: generate quotes in parallel #2847
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"math/big" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/synapsecns/sanguine/contrib/screener-api/client" | ||
|
@@ -324,26 +325,40 @@ | |
if err != nil { | ||
return nil, fmt.Errorf("error getting destination RFQ address: %w", err) | ||
} | ||
|
||
destTokenID := fmt.Sprintf("%d-%s", chainID, address.Hex()) | ||
|
||
// generate quotes in parallel | ||
g, gctx := errgroup.WithContext(ctx) | ||
quoteMtx := &sync.Mutex{} | ||
quotes = []model.PutQuoteRequest{} | ||
for keyTokenID, itemTokenIDs := range m.quotableTokens { | ||
for k, itemTokenIDs := range m.quotableTokens { | ||
for _, tokenID := range itemTokenIDs { | ||
//nolint:nestif | ||
if tokenID == destTokenID { | ||
quote, quoteErr := m.generateQuote(ctx, keyTokenID, chainID, address, balance, destRFQAddr) | ||
if quoteErr != nil { | ||
// continue generating quotes even if one fails | ||
span.AddEvent("error generating quote", trace.WithAttributes( | ||
attribute.String("key_token_id", keyTokenID), | ||
attribute.String("error", quoteErr.Error()), | ||
)) | ||
continue | ||
} | ||
quotes = append(quotes, *quote) | ||
keyTokenID := k | ||
g.Go(func() error { | ||
quote, quoteErr := m.generateQuote(gctx, keyTokenID, chainID, address, balance, destRFQAddr) | ||
if quoteErr != nil { | ||
// continue generating quotes even if one fails | ||
span.AddEvent("error generating quote", trace.WithAttributes( | ||
attribute.String("key_token_id", keyTokenID), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix variable scope issue in goroutine. The variable - span.AddEvent("error generating quote", trace.WithAttributes(
- attribute.String("key_token_id", keyTokenID),
+ span.AddEvent("error generating quote", trace.WithAttributes(
+ attribute.String("key_token_id", keyToken),
ToolsGitHub Check: Lint (services/rfq)
|
||
attribute.String("error", quoteErr.Error()), | ||
)) | ||
return nil | ||
} | ||
quoteMtx.Lock() | ||
defer quoteMtx.Unlock() | ||
quotes = append(quotes, *quote) | ||
return nil | ||
}) | ||
} | ||
} | ||
} | ||
err = g.Wait() | ||
if err != nil { | ||
return nil, fmt.Errorf("error generating quotes: %w", err) | ||
} | ||
|
||
m.currentQuotes = quotes | ||
return quotes, nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix variable scope issue in goroutine.
The variable
keyTokenID
from the range scope is used in the goroutine, which can lead to incorrect behavior.Committable suggestion
Tools
GitHub Check: Lint (services/rfq)