perf: run token price source lookups in parallel

Fetch alpha, futures, and spot sources concurrently in token lookup flow to reduce end-to-end response latency while preserving independent source behavior.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
2026-04-26 18:26:33 +07:00
parent 82b0f2f1a8
commit a36eb7aad0
2 changed files with 128 additions and 29 deletions
+54 -29
View File
@@ -2,6 +2,7 @@ package commands
import (
"strings"
"sync"
"gopkg.in/telebot.v3"
"me.thuanle/bbot/internal/configs/tele"
@@ -64,39 +65,63 @@ func showStickerMode(context telebot.Context, token string) {
func collectRichTokenData(token string) buildRichTokenMessageArgs {
a := buildRichTokenMessageArgs{Token: token}
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
a.HasAlpha = true
a.HasAlpha24h = true
a.Alpha24h = alphaToken.GetPercentChange24h()
if alphaPrice, ok := data.Market.GetAlphaPrice(token + "USDT"); ok {
a.AlphaPrice = alphaPrice
} else {
a.AlphaPrice = alphaToken.GetPrice()
}
}
futureSymbol := token + "USDT"
if data.Market.IsFuturesPair(futureSymbol) {
if fp, fr, ft, ok := data.Market.GetFuturePrice(futureSymbol); ok {
a.HasFuture = true
a.FuturePrice = fp
a.FundingRate = fr
a.FundingTimeMs = ft
}
}
spotSymbol := token + "USDT"
if data.Market.IsSpotPair(spotSymbol) {
if sp, ok := data.Market.GetSpotPrice(spotSymbol); ok {
a.HasSpot = true
a.SpotPrice = sp
}
}
marginRates := data.Market.GetMarginInterestRates()
a.MarginAPRPercent = marginRates[token] * 365 * 100
a.HasMarginAPR = marginRates[token] != 0
futureSymbol := token + "USDT"
spotSymbol := token + "USDT"
var (
wg sync.WaitGroup
mu sync.Mutex
)
wg.Add(3)
go func() {
defer wg.Done()
if alphaToken, ok := data.Market.GetAlphaToken(token); ok {
alpha24h := alphaToken.GetPercentChange24h()
alphaPrice := alphaToken.GetPrice()
if p, ok := data.Market.GetAlphaPrice(token + "USDT"); ok {
alphaPrice = p
}
mu.Lock()
a.HasAlpha = true
a.HasAlpha24h = true
a.Alpha24h = alpha24h
a.AlphaPrice = alphaPrice
mu.Unlock()
}
}()
go func() {
defer wg.Done()
if data.Market.IsFuturesPair(futureSymbol) {
if fp, fr, ft, ok := data.Market.GetFuturePrice(futureSymbol); ok {
mu.Lock()
a.HasFuture = true
a.FuturePrice = fp
a.FundingRate = fr
a.FundingTimeMs = ft
mu.Unlock()
}
}
}()
go func() {
defer wg.Done()
if data.Market.IsSpotPair(spotSymbol) {
if sp, ok := data.Market.GetSpotPrice(spotSymbol); ok {
mu.Lock()
a.HasSpot = true
a.SpotPrice = sp
mu.Unlock()
}
}
}()
wg.Wait()
return a
}