@@ -12,27 +12,25 @@ type Service struct {
12
12
ctx context.Context
13
13
cancel context.CancelFunc
14
14
15
- startTickerWithKline bool
16
- startOrderbookWithKline bool
15
+ startTickerWithKline bool
16
+ startDepthWithKline bool
17
17
18
18
class Class
19
19
exchangeInfoSrv * ExchangeInfoSrv
20
20
klinesSrv sync.Map // map[symbolInterval]*Klines
21
21
depthSrv sync.Map // map[symbolInterval]*Depth
22
22
tickerSrv sync.Map // map[symbolInterval]*Ticker
23
23
24
- klineIntervalMap sync.Map // map[string]string
25
-
26
24
lastGetKlines sync.Map // map[symbolInterval]time.Time
27
25
lastGetDepth sync.Map // map[symbolInterval]time.Time
28
26
lastGetTicker sync.Map // map[symbolInterval]time.Time
29
27
}
30
28
31
- func NewService (ctx context.Context , class Class , startTickerWithKline , startOrderbookWithKline bool ) * Service {
29
+ func NewService (ctx context.Context , class Class , startTickerWithKline , startDepthWithKline bool ) * Service {
32
30
s := & Service {
33
- class : class ,
34
- startTickerWithKline : startTickerWithKline ,
35
- startOrderbookWithKline : startOrderbookWithKline ,
31
+ class : class ,
32
+ startTickerWithKline : startTickerWithKline ,
33
+ startDepthWithKline : startDepthWithKline ,
36
34
}
37
35
s .ctx , s .cancel = context .WithCancel (ctx )
38
36
s .exchangeInfoSrv = NewExchangeInfoSrv (s .ctx , NewSymbolInterval (s .class , "" , "" ))
@@ -59,10 +57,13 @@ func NewService(ctx context.Context, class Class, startTickerWithKline, startOrd
59
57
}
60
58
61
59
func (s * Service ) autoRemoveExpired () {
60
+ var aliveKlines = make (map [string ]struct {})
62
61
s .klinesSrv .Range (func (k , v interface {}) bool {
63
62
si := k .(symbolInterval )
64
63
srv := v .(* KlinesSrv )
65
64
65
+ aliveKlines [si .Symbol ] = struct {}{}
66
+
66
67
if t , ok := s .lastGetKlines .Load (si ); ok {
67
68
if time .Now ().Sub (t .(time.Time )) > 2 * INTERVAL_2_DURATION [si .Interval ] {
68
69
log .Debugf ("%s.Kline srv expired!Removed" , si )
@@ -82,7 +83,9 @@ func (s *Service) autoRemoveExpired() {
82
83
srv := v .(* DepthSrv )
83
84
84
85
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 {
86
89
log .Debugf ("%s.Depth srv expired!Removed" , si )
87
90
s .lastGetDepth .Delete (si )
88
91
@@ -100,7 +103,9 @@ func (s *Service) autoRemoveExpired() {
100
103
srv := v .(* TickerSrv )
101
104
102
105
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 {
104
109
log .Debugf ("%s.Ticker srv expired!Removed" , si )
105
110
s .lastGetTicker .Delete (si )
106
111
@@ -117,15 +122,11 @@ func (s *Service) autoRemoveExpired() {
117
122
118
123
func (s * Service ) Ticker (symbol string ) * Ticker24hr {
119
124
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
+
126
127
s .lastGetTicker .Store (* si , time .Now ())
127
128
128
- return srv .( * TickerSrv ). GetTicker ()
129
+ return srv .GetTicker ()
129
130
}
130
131
131
132
func (s * Service ) ExchangeInfo () []byte {
@@ -134,27 +135,54 @@ func (s *Service) ExchangeInfo() []byte {
134
135
135
136
func (s * Service ) Klines (symbol , interval string ) []* Kline {
136
137
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 , "" ))
142
144
}
143
145
144
146
s .lastGetKlines .Store (* si , time .Now ())
145
147
146
- return srv .( * KlinesSrv ). GetKlines ()
148
+ return srv .GetKlines ()
147
149
}
148
150
149
151
func (s * Service ) Depth (symbol string ) * Depth {
150
152
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 {
151
171
srv , loaded := s .depthSrv .Load (* si )
152
172
if ! loaded {
153
173
if srv , loaded = s .depthSrv .LoadOrStore (* si , NewDepthSrv (s .ctx , si )); loaded == false {
154
174
srv .(* DepthSrv ).Start ()
155
175
}
156
176
}
157
- s .lastGetDepth .Store (* si , time .Now ())
177
+ return srv .(* DepthSrv )
178
+ }
158
179
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 )
160
188
}
0 commit comments