Skip to content

Commit 63b684a

Browse files
committed
improve auto remove logic
1 parent 9d4a5b5 commit 63b684a

File tree

4 files changed

+63
-35
lines changed

4 files changed

+63
-35
lines changed

src/binance-proxy/handler/handler.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ import (
1212

1313
func NewHandler(
1414
ctx context.Context, class service.Class,
15-
enableFakeKline, startTickerWithKline, startOrderbookWithKline bool,
15+
enableFakeKline, startTickerWithKline, startDepthWithKline bool,
1616
) func(w http.ResponseWriter, r *http.Request) {
1717
handler := &Handler{
18-
srv: service.NewService(ctx, class, startTickerWithKline, startOrderbookWithKline),
18+
srv: service.NewService(ctx, class, startTickerWithKline, startDepthWithKline),
1919
class: class,
2020
enableFakeKline: enableFakeKline,
2121
}

src/binance-proxy/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717

1818
func startProxy(ctx context.Context, address string, class service.Class) {
1919
mux := http.NewServeMux()
20-
mux.HandleFunc("/", handler.NewHandler(ctx, class, flagEnableFakeKline, flagStartTickerWithKline, flagStartOrderbookWithKline))
20+
mux.HandleFunc("/", handler.NewHandler(ctx, class, flagEnableFakeKline, flagStartTickerWithKline, flagStartDepthWithKline))
2121

2222
log.Infof("Start %s proxy !Address: %s", class, address)
2323
if err := http.ListenAndServe(address, mux); err != nil {
@@ -42,14 +42,14 @@ var flagFuturesAddress string
4242
var flagDebug bool
4343
var flagEnableFakeKline bool
4444
var flagStartTickerWithKline bool
45-
var flagStartOrderbookWithKline bool
45+
var flagStartDepthWithKline bool
4646

4747
func main() {
4848
flag.StringVar(&flagSpotAddress, "s", ":8090", "spot bind address.")
4949
flag.StringVar(&flagFuturesAddress, "f", ":8091", "futures bind address.")
5050
flag.BoolVar(&flagEnableFakeKline, "fakekline", false, "enable fake kline.")
5151
flag.BoolVar(&flagStartTickerWithKline, "at", false, "auto monitor ticker with kline request.")
52-
flag.BoolVar(&flagStartOrderbookWithKline, "ao", false, "auto monitor orderbook with kline request.")
52+
flag.BoolVar(&flagStartDepthWithKline, "ao", false, "auto monitor depth with kline request.")
5353
flag.BoolVar(&flagDebug, "v", false, "print debug log.")
5454
flag.Parse()
5555

src/binance-proxy/service/service.go

+53-25
Original file line numberDiff line numberDiff line change
@@ -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

6159
func (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

118123
func (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

131132
func (s *Service) ExchangeInfo() []byte {
@@ -134,27 +135,54 @@ func (s *Service) ExchangeInfo() []byte {
134135

135136
func (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

149151
func (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
}

src/binance-proxy/service/ticker.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,16 @@ func (s *TickerSrv) Start() {
7171
s.bookTicker = nil
7272
s.rw.Unlock()
7373

74-
ticker24hrDoneC, ticker24hrstopC, err := s.connectTicker24hr()
74+
bookDoneC, bookStopC, err := s.connectTickerBook()
7575
if err != nil {
76-
log.Errorf("%s.Websocket 24hr ticker connect error!Error:%s", s.si, err)
76+
bookStopC <- struct{}{}
77+
log.Errorf("%s.Websocket book ticker connect error!Error:%s", s.si, err)
7778
continue
7879
}
7980

80-
bookDoneC, bookStopC, err := s.connectTickerBook()
81+
ticker24hrDoneC, ticker24hrstopC, err := s.connectTicker24hr()
8182
if err != nil {
82-
bookStopC <- struct{}{}
83-
log.Errorf("%s.Websocket book ticker connect error!Error:%s", s.si, err)
83+
log.Errorf("%s.Websocket 24hr ticker connect error!Error:%s", s.si, err)
8484
continue
8585
}
8686

0 commit comments

Comments
 (0)