@@ -12,27 +12,25 @@ type Service struct {
1212 ctx context.Context
1313 cancel context.CancelFunc
1414
15- startTickerWithKline bool
16- startOrderbookWithKline bool
15+ startTickerWithKline bool
16+ startDepthWithKline bool
1717
1818 class Class
1919 exchangeInfoSrv * ExchangeInfoSrv
2020 klinesSrv sync.Map // map[symbolInterval]*Klines
2121 depthSrv sync.Map // map[symbolInterval]*Depth
2222 tickerSrv sync.Map // map[symbolInterval]*Ticker
2323
24- klineIntervalMap sync.Map // map[string]string
25-
2624 lastGetKlines sync.Map // map[symbolInterval]time.Time
2725 lastGetDepth sync.Map // map[symbolInterval]time.Time
2826 lastGetTicker sync.Map // map[symbolInterval]time.Time
2927}
3028
31- func NewService (ctx context.Context , class Class , startTickerWithKline , startOrderbookWithKline bool ) * Service {
29+ func NewService (ctx context.Context , class Class , startTickerWithKline , startDepthWithKline bool ) * Service {
3230 s := & Service {
33- class : class ,
34- startTickerWithKline : startTickerWithKline ,
35- startOrderbookWithKline : startOrderbookWithKline ,
31+ class : class ,
32+ startTickerWithKline : startTickerWithKline ,
33+ startDepthWithKline : startDepthWithKline ,
3634 }
3735 s .ctx , s .cancel = context .WithCancel (ctx )
3836 s .exchangeInfoSrv = NewExchangeInfoSrv (s .ctx , NewSymbolInterval (s .class , "" , "" ))
@@ -59,10 +57,13 @@ func NewService(ctx context.Context, class Class, startTickerWithKline, startOrd
5957}
6058
6159func (s * Service ) autoRemoveExpired () {
60+ var aliveKlines = make (map [string ]struct {})
6261 s .klinesSrv .Range (func (k , v interface {}) bool {
6362 si := k .(symbolInterval )
6463 srv := v .(* KlinesSrv )
6564
65+ aliveKlines [si .Symbol ] = struct {}{}
66+
6667 if t , ok := s .lastGetKlines .Load (si ); ok {
6768 if time .Now ().Sub (t .(time.Time )) > 2 * INTERVAL_2_DURATION [si .Interval ] {
6869 log .Debugf ("%s.Kline srv expired!Removed" , si )
@@ -82,7 +83,9 @@ func (s *Service) autoRemoveExpired() {
8283 srv := v .(* DepthSrv )
8384
8485 if t , ok := s .lastGetDepth .Load (si ); ok {
85- if time .Now ().Sub (t .(time.Time )) > 2 * time .Minute {
86+ _ , isKlineAlive := aliveKlines [si .Symbol ]
87+
88+ if ((s .startDepthWithKline && ! isKlineAlive ) || ! s .startDepthWithKline ) && time .Now ().Sub (t .(time.Time )) > 2 * time .Minute {
8689 log .Debugf ("%s.Depth srv expired!Removed" , si )
8790 s .lastGetDepth .Delete (si )
8891
@@ -100,7 +103,9 @@ func (s *Service) autoRemoveExpired() {
100103 srv := v .(* TickerSrv )
101104
102105 if t , ok := s .lastGetTicker .Load (si ); ok {
103- if time .Now ().Sub (t .(time.Time )) > 2 * time .Minute {
106+ _ , isKlineAlive := aliveKlines [si .Symbol ]
107+
108+ if ((s .startTickerWithKline && ! isKlineAlive ) || ! s .startTickerWithKline ) && time .Now ().Sub (t .(time.Time )) > 2 * time .Minute {
104109 log .Debugf ("%s.Ticker srv expired!Removed" , si )
105110 s .lastGetTicker .Delete (si )
106111
@@ -117,15 +122,11 @@ func (s *Service) autoRemoveExpired() {
117122
118123func (s * Service ) Ticker (symbol string ) * Ticker24hr {
119124 si := NewSymbolInterval (s .class , symbol , "" )
120- srv , loaded := s .tickerSrv .Load (* si )
121- if ! loaded {
122- if srv , loaded = s .tickerSrv .LoadOrStore (* si , NewTickerSrv (s .ctx , si )); loaded == false {
123- srv .(* TickerSrv ).Start ()
124- }
125- }
125+ srv := s .StartTickerSrv (si )
126+
126127 s .lastGetTicker .Store (* si , time .Now ())
127128
128- return srv .( * TickerSrv ). GetTicker ()
129+ return srv .GetTicker ()
129130}
130131
131132func (s * Service ) ExchangeInfo () []byte {
@@ -134,27 +135,54 @@ func (s *Service) ExchangeInfo() []byte {
134135
135136func (s * Service ) Klines (symbol , interval string ) []* Kline {
136137 si := NewSymbolInterval (s .class , symbol , interval )
137- srv , loaded := s .klinesSrv .Load (* si )
138- if ! loaded {
139- if srv , loaded = s .klinesSrv .LoadOrStore (* si , NewKlinesSrv (s .ctx , si )); loaded == false {
140- srv .(* KlinesSrv ).Start ()
141- }
138+ srv := s .StartKlineSrv (si )
139+ if s .startTickerWithKline {
140+ s .StartTickerSrv (NewSymbolInterval (s .class , symbol , "" ))
141+ }
142+ if s .startDepthWithKline {
143+ s .StartDepthSrv (NewSymbolInterval (s .class , symbol , "" ))
142144 }
143145
144146 s .lastGetKlines .Store (* si , time .Now ())
145147
146- return srv .( * KlinesSrv ). GetKlines ()
148+ return srv .GetKlines ()
147149}
148150
149151func (s * Service ) Depth (symbol string ) * Depth {
150152 si := NewSymbolInterval (s .class , symbol , "" )
153+ srv := s .StartDepthSrv (si )
154+
155+ s .lastGetDepth .Store (* si , time .Now ())
156+
157+ return srv .GetDepth ()
158+ }
159+
160+ func (s * Service ) StartKlineSrv (si * symbolInterval ) * KlinesSrv {
161+ srv , loaded := s .klinesSrv .Load (* si )
162+ if ! loaded {
163+ if srv , loaded = s .klinesSrv .LoadOrStore (* si , NewKlinesSrv (s .ctx , si )); loaded == false {
164+ srv .(* KlinesSrv ).Start ()
165+ }
166+ }
167+ return srv .(* KlinesSrv )
168+ }
169+
170+ func (s * Service ) StartDepthSrv (si * symbolInterval ) * DepthSrv {
151171 srv , loaded := s .depthSrv .Load (* si )
152172 if ! loaded {
153173 if srv , loaded = s .depthSrv .LoadOrStore (* si , NewDepthSrv (s .ctx , si )); loaded == false {
154174 srv .(* DepthSrv ).Start ()
155175 }
156176 }
157- s .lastGetDepth .Store (* si , time .Now ())
177+ return srv .(* DepthSrv )
178+ }
158179
159- return srv .(* DepthSrv ).GetDepth ()
180+ func (s * Service ) StartTickerSrv (si * symbolInterval ) * TickerSrv {
181+ srv , loaded := s .tickerSrv .Load (* si )
182+ if ! loaded {
183+ if srv , loaded = s .tickerSrv .LoadOrStore (* si , NewTickerSrv (s .ctx , si )); loaded == false {
184+ srv .(* TickerSrv ).Start ()
185+ }
186+ }
187+ return srv .(* TickerSrv )
160188}
0 commit comments