|
1 | 1 | package service
|
2 | 2 |
|
3 |
| -import "golang.org/x/time/rate" |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/url" |
| 7 | + "strconv" |
4 | 8 |
|
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