Skip to content

Commit 8431e5a

Browse files
committed
fix 24ticker empty symbol.Improve the weight list
1 parent 13f482f commit 8431e5a

File tree

6 files changed

+93
-14
lines changed

6 files changed

+93
-14
lines changed

src/binance-proxy/handler/handler.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ func NewHandler(ctx context.Context, class service.Class, enableFakeKline bool)
1616
class: class,
1717
enableFakeKline: enableFakeKline,
1818
}
19+
handler.ctx, handler.cancel = context.WithCancel(ctx)
1920

2021
return handler.Router
2122
}
2223

2324
type Handler struct {
25+
ctx context.Context
26+
cancel context.CancelFunc
27+
2428
class service.Class
2529
srv *service.Service
2630
enableFakeKline bool
@@ -48,6 +52,8 @@ func (s *Handler) Router(w http.ResponseWriter, r *http.Request) {
4852
func (s *Handler) reverseProxy(w http.ResponseWriter, r *http.Request) {
4953
log.Debugf("%s reverse proxy.Path:%s", s.class, r.URL.RequestURI())
5054

55+
service.RateWait(s.ctx, s.class, r.Method, r.URL.Path, r.URL.Query())
56+
5157
var u *url.URL
5258
if s.class == service.SPOT {
5359
r.Host = "api.binance.com"
@@ -57,12 +63,6 @@ func (s *Handler) reverseProxy(w http.ResponseWriter, r *http.Request) {
5763
u, _ = url.Parse("https://fapi.binance.com")
5864
}
5965

60-
if s.class == service.SPOT {
61-
service.SpotLimiter.WaitN(context.Background(), 1)
62-
} else {
63-
service.FuturesLimiter.WaitN(context.Background(), 5)
64-
}
65-
6666
proxy := httputil.NewSingleHostReverseProxy(u)
6767

6868
proxy.ServeHTTP(w, r)

src/binance-proxy/handler/kline.go

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ func (s *Handler) klines(w http.ResponseWriter, r *http.Request) {
2020
case err != nil, limitInt <= 0, limitInt > 1000,
2121
r.URL.Query().Get("startTime") != "", r.URL.Query().Get("endTime") != "",
2222
symbol == "", interval == "":
23-
2423
s.reverseProxy(w, r)
2524
return
2625
}

src/binance-proxy/handler/ticker.go

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
func (s *Handler) ticker(w http.ResponseWriter, r *http.Request) {
99
symbol := r.URL.Query().Get("symbol")
1010

11+
if symbol == "" {
12+
s.reverseProxy(w, r)
13+
return
14+
}
1115
ticker := s.srv.Ticker(symbol)
1216
if ticker == nil {
1317
s.reverseProxy(w, r)

src/binance-proxy/service/exchangeinfo.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ func (s *ExchangeInfoSrv) refreshExchangeInfo() error {
7979
var url string
8080
if s.si.Class == SPOT {
8181
url = "https://api.binance.com/api/v3/exchangeInfo"
82-
SpotLimiter.WaitN(s.ctx, 1)
82+
RateWait(s.ctx, s.si.Class, http.MethodGet, "/api/v3/exchangeInfo", nil)
8383
} else {
8484
url = "https://fapi.binance.com/fapi/v1/exchangeInfo"
85-
FuturesLimiter.WaitN(s.ctx, 5)
85+
RateWait(s.ctx, s.si.Class, http.MethodGet, "/fapi/v1/exchangeInfo", nil)
8686
}
8787

8888
resp, err := http.Get(url)

src/binance-proxy/service/kline.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"binance-proxy/tool"
55
"container/list"
66
"context"
7+
"net/http"
8+
"net/url"
79
"sync"
810

911
log "github.com/sirupsen/logrus"
@@ -103,12 +105,16 @@ func (s *KlinesSrv) initKlineData() {
103105
var err error
104106
for d := tool.NewDelayIterator(); ; d.Delay() {
105107
if s.si.Class == SPOT {
106-
SpotLimiter.WaitN(s.ctx, 1)
108+
RateWait(s.ctx, s.si.Class, http.MethodGet, "/api/v3/klines", url.Values{
109+
"limit": []string{"1000"},
110+
})
107111
klines, err = spot.NewClient("", "").NewKlinesService().
108112
Symbol(s.si.Symbol).Interval(s.si.Interval).Limit(1000).
109113
Do(s.ctx)
110114
} else {
111-
FuturesLimiter.WaitN(s.ctx, 5)
115+
RateWait(s.ctx, s.si.Class, http.MethodGet, "/fapi/v1/klines", url.Values{
116+
"limit": []string{"1000"},
117+
})
112118
klines, err = futures.NewClient("", "").NewKlinesService().
113119
Symbol(s.si.Symbol).Interval(s.si.Interval).Limit(1000).
114120
Do(s.ctx)

src/binance-proxy/service/limiter.go

+73-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,76 @@
11
package service
22

3-
import "golang.org/x/time/rate"
3+
import (
4+
"context"
5+
"net/http"
6+
"net/url"
7+
"strconv"
48

5-
var SpotLimiter = rate.NewLimiter(20, 1200)
6-
var FuturesLimiter = rate.NewLimiter(40, 2400)
9+
"golang.org/x/time/rate"
10+
)
11+
12+
var (
13+
SpotLimiter = rate.NewLimiter(20, 1200)
14+
FuturesLimiter = rate.NewLimiter(40, 2400)
15+
)
16+
17+
func RateWait(ctx context.Context, class Class, method, path string, query url.Values) {
18+
weight := 1
19+
switch path {
20+
case "/fapi/v1/klines":
21+
weight = 5
22+
limitInt, _ := strconv.Atoi(query.Get("limit"))
23+
if limitInt >= 1 && limitInt < 100 {
24+
weight = 1
25+
} else if limitInt >= 100 && limitInt < 500 {
26+
weight = 2
27+
} else if limitInt >= 500 && limitInt <= 1000 {
28+
weight = 5
29+
} else if limitInt > 1000 && limitInt <= 1500 {
30+
weight = 10
31+
}
32+
case "/api/v3/depth":
33+
limitInt, _ := strconv.Atoi(query.Get("limit"))
34+
if limitInt >= 5 && limitInt <= 100 {
35+
weight = 1
36+
} else if limitInt >= 100 && limitInt < 500 {
37+
weight = 2
38+
} else if limitInt == 500 {
39+
weight = 5
40+
} else if limitInt == 1000 {
41+
weight = 10
42+
} else if limitInt == 5000 {
43+
weight = 50
44+
}
45+
case "/fapi/v1/depth":
46+
limitInt, _ := strconv.Atoi(query.Get("limit"))
47+
if limitInt >= 5 && limitInt <= 50 {
48+
weight = 2
49+
} else if limitInt == 100 {
50+
weight = 5
51+
} else if limitInt == 500 {
52+
weight = 10
53+
} else if limitInt == 1000 {
54+
weight = 20
55+
}
56+
case "/api/v3/ticker/24hr", "/fapi/v1/ticker/24hr":
57+
if query.Get("symbol") == "" {
58+
weight = 40
59+
}
60+
case "/api/v3/exchangeInfo", "/fapi/v1/exchangeInfo", "/api/v3/account", "/api/v3/myTrades":
61+
weight = 10
62+
case "/api/v3/order":
63+
if method == http.MethodGet {
64+
weight = 2
65+
}
66+
case "/fapi/v1/userTrades", "/fapi/v2/account":
67+
weight = 5
68+
69+
}
70+
71+
if class == SPOT {
72+
SpotLimiter.WaitN(ctx, weight)
73+
} else {
74+
FuturesLimiter.WaitN(ctx, weight)
75+
}
76+
}

0 commit comments

Comments
 (0)