Skip to content

Commit c8aa4f5

Browse files
committed
Less than 1000candles use the cache
1 parent bd70837 commit c8aa4f5

File tree

4 files changed

+42
-82
lines changed

4 files changed

+42
-82
lines changed

src/binance-proxy/handler/handler.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@ import (
1010
log "github.com/sirupsen/logrus"
1111
)
1212

13-
func NewHandler(
14-
ctx context.Context, class service.Class,
15-
enableFakeKline, startTickerWithKline, startDepthWithKline bool,
16-
) func(w http.ResponseWriter, r *http.Request) {
13+
func NewHandler(ctx context.Context, class service.Class) func(w http.ResponseWriter, r *http.Request) {
1714
handler := &Handler{
18-
srv: service.NewService(ctx, class, startTickerWithKline, startDepthWithKline),
19-
class: class,
20-
enableFakeKline: enableFakeKline,
15+
srv: service.NewService(ctx, class),
16+
class: class,
2117
}
2218
handler.ctx, handler.cancel = context.WithCancel(ctx)
2319

@@ -28,9 +24,8 @@ type Handler struct {
2824
ctx context.Context
2925
cancel context.CancelFunc
3026

31-
class service.Class
32-
srv *service.Service
33-
enableFakeKline bool
27+
class service.Class
28+
srv *service.Service
3429
}
3530

3631
func (s *Handler) Router(w http.ResponseWriter, r *http.Request) {

src/binance-proxy/handler/kline.go

+32-44
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,59 @@ import (
55
"net/http"
66
"strconv"
77
"time"
8+
9+
"binance-proxy/service"
810
)
911

1012
func (s *Handler) klines(w http.ResponseWriter, r *http.Request) {
1113
symbol := r.URL.Query().Get("symbol")
1214
interval := r.URL.Query().Get("interval")
13-
limit := r.URL.Query().Get("limit")
14-
if limit == "" {
15-
limit = "500"
15+
limitInt, _ := strconv.Atoi(r.URL.Query().Get("limit"))
16+
if limitInt == 0 {
17+
limitInt = 500
1618
}
17-
limitInt, err := strconv.Atoi(limit)
19+
20+
startTimeUnix, _ := strconv.Atoi(r.URL.Query().Get("startTime"))
21+
startTime := time.Unix(int64(startTimeUnix/1000), 0)
1822

1923
switch {
20-
case err != nil, limitInt <= 0, limitInt > 1000,
21-
r.URL.Query().Get("startTime") != "", r.URL.Query().Get("endTime") != "",
24+
case limitInt <= 0, limitInt > 1000,
25+
startTime.Unix() > 0 && startTime.Before(time.Now().Add(service.INTERVAL_2_DURATION[interval]*999*-1)),
26+
r.URL.Query().Get("endTime") != "",
2227
symbol == "", interval == "":
2328
s.reverseProxy(w, r)
2429
return
2530
}
2631

2732
data := s.srv.Klines(symbol, interval)
28-
if data == nil {
29-
s.reverseProxy(w, r)
30-
return
31-
}
32-
33-
minLen := len(data)
34-
if minLen > limitInt {
35-
minLen = limitInt
33+
klines := make([]interface{}, 0)
34+
startTimeUnixMs := startTime.Unix() * 1000
35+
if startTimeUnixMs == 0 && limitInt < len(data) {
36+
data = data[len(data)-limitInt:]
3637
}
38+
for _, v := range data {
39+
if len(klines) >= limitInt {
40+
break
41+
}
3742

38-
klines := make([]interface{}, minLen)
39-
for i := minLen; i > 0; i-- {
40-
ri := len(data) - i
41-
klines[minLen-i] = []interface{}{
42-
data[ri].OpenTime,
43-
data[ri].Open,
44-
data[ri].High,
45-
data[ri].Low,
46-
data[ri].Close,
47-
data[ri].Volume,
48-
data[ri].CloseTime,
49-
data[ri].QuoteAssetVolume,
50-
data[ri].TradeNum,
51-
data[ri].TakerBuyBaseAssetVolume,
52-
data[ri].TakerBuyQuoteAssetVolume,
53-
"0",
43+
if startTimeUnixMs > 0 && startTimeUnixMs > v.OpenTime {
44+
continue
5445
}
55-
}
5646

57-
if s.enableFakeKline && len(data) > 0 && time.Now().UnixNano()/1e6 > data[len(data)-1].CloseTime {
5847
klines = append(klines, []interface{}{
59-
data[len(data)-1].CloseTime + 1,
60-
data[len(data)-1].Close,
61-
data[len(data)-1].Close,
62-
data[len(data)-1].Close,
63-
data[len(data)-1].Close,
64-
"0.0",
65-
data[len(data)-1].CloseTime + 1 + (data[len(data)-1].CloseTime - data[len(data)-1].OpenTime),
66-
"0.0",
67-
0,
68-
"0.0",
69-
"0.0",
48+
v.OpenTime,
49+
v.Open,
50+
v.High,
51+
v.Low,
52+
v.Close,
53+
v.Volume,
54+
v.CloseTime,
55+
v.QuoteAssetVolume,
56+
v.TradeNum,
57+
v.TakerBuyBaseAssetVolume,
58+
v.TakerBuyQuoteAssetVolume,
7059
"0",
7160
})
72-
klines = klines[len(klines)-minLen:]
7361
}
7462

7563
w.Header().Set("Content-Type", "application/json")

src/binance-proxy/main.go

+1-7
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, flagStartDepthWithKline))
20+
mux.HandleFunc("/", handler.NewHandler(ctx, class))
2121

2222
log.Infof("Start %s proxy !Address: %s", class, address)
2323
if err := http.ListenAndServe(address, mux); err != nil {
@@ -40,16 +40,10 @@ var ctx, cancel = context.WithCancel(context.Background())
4040
var flagSpotAddress string
4141
var flagFuturesAddress string
4242
var flagDebug bool
43-
var flagEnableFakeKline bool
44-
var flagStartTickerWithKline bool
45-
var flagStartDepthWithKline bool
4643

4744
func main() {
4845
flag.StringVar(&flagSpotAddress, "s", ":8090", "spot bind address.")
4946
flag.StringVar(&flagFuturesAddress, "f", ":8091", "futures bind address.")
50-
flag.BoolVar(&flagEnableFakeKline, "fakekline", false, "enable fake kline.")
51-
flag.BoolVar(&flagStartTickerWithKline, "at", false, "auto monitor ticker with kline request.")
52-
flag.BoolVar(&flagStartDepthWithKline, "ao", false, "auto monitor depth with kline request.")
5347
flag.BoolVar(&flagDebug, "v", false, "print debug log.")
5448
flag.Parse()
5549

src/binance-proxy/service/service.go

+4-21
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ type Service struct {
1212
ctx context.Context
1313
cancel context.CancelFunc
1414

15-
startTickerWithKline bool
16-
startDepthWithKline bool
17-
1815
class Class
1916
exchangeInfoSrv *ExchangeInfoSrv
2017
klinesSrv sync.Map // map[symbolInterval]*Klines
@@ -26,12 +23,8 @@ type Service struct {
2623
lastGetTicker sync.Map // map[symbolInterval]time.Time
2724
}
2825

29-
func NewService(ctx context.Context, class Class, startTickerWithKline, startDepthWithKline bool) *Service {
30-
s := &Service{
31-
class: class,
32-
startTickerWithKline: startTickerWithKline,
33-
startDepthWithKline: startDepthWithKline,
34-
}
26+
func NewService(ctx context.Context, class Class) *Service {
27+
s := &Service{class: class}
3528
s.ctx, s.cancel = context.WithCancel(ctx)
3629
s.exchangeInfoSrv = NewExchangeInfoSrv(s.ctx, NewSymbolInterval(s.class, "", ""))
3730
s.exchangeInfoSrv.Start()
@@ -83,9 +76,7 @@ func (s *Service) autoRemoveExpired() {
8376
srv := v.(*DepthSrv)
8477

8578
if t, ok := s.lastGetDepth.Load(si); ok {
86-
_, isKlineAlive := aliveKlines[si.Symbol]
87-
88-
if ((s.startDepthWithKline && !isKlineAlive) || !s.startDepthWithKline) && time.Now().Sub(t.(time.Time)) > 2*time.Minute {
79+
if time.Now().Sub(t.(time.Time)) > 2*time.Minute {
8980
log.Debugf("%s.Depth srv expired!Removed", si)
9081
s.lastGetDepth.Delete(si)
9182

@@ -103,9 +94,7 @@ func (s *Service) autoRemoveExpired() {
10394
srv := v.(*TickerSrv)
10495

10596
if t, ok := s.lastGetTicker.Load(si); ok {
106-
_, isKlineAlive := aliveKlines[si.Symbol]
107-
108-
if ((s.startTickerWithKline && !isKlineAlive) || !s.startTickerWithKline) && time.Now().Sub(t.(time.Time)) > 2*time.Minute {
97+
if time.Now().Sub(t.(time.Time)) > 2*time.Minute {
10998
log.Debugf("%s.Ticker srv expired!Removed", si)
11099
s.lastGetTicker.Delete(si)
111100

@@ -136,12 +125,6 @@ func (s *Service) ExchangeInfo() []byte {
136125
func (s *Service) Klines(symbol, interval string) []*Kline {
137126
si := NewSymbolInterval(s.class, symbol, interval)
138127
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, ""))
144-
}
145128

146129
s.lastGetKlines.Store(*si, time.Now())
147130

0 commit comments

Comments
 (0)