Files
crypto-price-bot/internal/data/market/main.go
T
thuanle 440fa3bacd 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>
2026-04-25 00:15:22 +07:00

42 lines
916 B
Go

package market
import (
"sync"
"time"
"github.com/rs/zerolog/log"
)
type MarketData struct {
mu sync.RWMutex
futureMarkPrice map[string]float64
futureFundingRate map[string]float64
futureNextFundingTime map[string]int64
spotPrice map[string]float64
// Alpha token cache
alphaTokens map[string]AlphaTokenInfo
alphaCacheMutex sync.RWMutex
lastAlphaCacheUpdate time.Time
}
func NewMarketData() *MarketData {
log.Info().Msg("Start market service")
ms := &MarketData{
futureMarkPrice: make(map[string]float64),
futureFundingRate: make(map[string]float64),
futureNextFundingTime: make(map[string]int64),
spotPrice: make(map[string]float64),
alphaTokens: make(map[string]AlphaTokenInfo),
}
_ = ms.StartFutureWsMarkPrice()
_ = ms.StartSpotWsMarkPrice()
// Initialize Alpha token cache
go ms.refreshAlphaTokenCache()
return ms
}