Fix concurrent map read/write race condition in MarketData
Add sync.RWMutex to protect future and spot price maps accessed by WebSocket handlers (writers) and Telegram bot handlers (readers). Also adds Alpha token support with caching. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,14 +1,35 @@
|
||||
package market
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/adshao/go-binance/v2"
|
||||
"github.com/rs/zerolog/log"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (ms *MarketData) GetSpotPrice(symbol string) (float64, bool) {
|
||||
ms.mu.RLock()
|
||||
p, ok := ms.spotPrice[symbol]
|
||||
return p, ok
|
||||
ms.mu.RUnlock()
|
||||
if ok {
|
||||
return p, true
|
||||
}
|
||||
|
||||
// If not found, check if it's an Alpha token and ensure cache is loaded
|
||||
ms.ensureAlphaCacheLoaded()
|
||||
if ms.IsAlphaToken(symbol) {
|
||||
if alphaToken, exists := ms.GetAlphaToken(symbol); exists {
|
||||
price := alphaToken.GetPrice()
|
||||
if price > 0 {
|
||||
ms.mu.Lock()
|
||||
ms.spotPrice[symbol] = price
|
||||
ms.mu.Unlock()
|
||||
return price, true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false
|
||||
}
|
||||
|
||||
func (ms *MarketData) StartSpotWsMarkPrice() error {
|
||||
@@ -21,6 +42,8 @@ func (ms *MarketData) StartSpotWsMarkPrice() error {
|
||||
}
|
||||
|
||||
func (ms *MarketData) spotWsAllMarketsStatHandler(event binance.WsAllMarketsStatEvent) {
|
||||
ms.mu.Lock()
|
||||
defer ms.mu.Unlock()
|
||||
for _, priceEvent := range event {
|
||||
price, err := strconv.ParseFloat(priceEvent.LastPrice, 64)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user