-
Notifications
You must be signed in to change notification settings - Fork 789
/
ratelimit.go
66 lines (59 loc) · 1.89 KB
/
ratelimit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package btcmarkets
import (
"context"
"time"
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
"golang.org/x/time/rate"
)
// BTCMarkets Rate limit consts
const (
btcmarketsRateInterval = time.Second * 10
btcmarketsAuthLimit = 50
btcmarketsUnauthLimit = 50
btcmarketsOrderLimit = 30
btcmarketsBatchOrderLimit = 5
btcmarketsWithdrawLimit = 10
btcmarketsCreateNewReportLimit = 1
// Used to match endpoints to rate limits
orderFunc request.EndpointLimit = iota
batchFunc
withdrawFunc
newReportFunc
)
// RateLimit implements the request.Limiter interface
type RateLimit struct {
Auth *rate.Limiter
UnAuth *rate.Limiter
OrderPlacement *rate.Limiter
BatchOrders *rate.Limiter
WithdrawRequest *rate.Limiter
CreateNewReport *rate.Limiter
}
// Limit limits the outbound requests
func (r *RateLimit) Limit(ctx context.Context, f request.EndpointLimit) error {
switch f {
case request.Auth:
return r.Auth.Wait(ctx)
case orderFunc:
return r.OrderPlacement.Wait(ctx)
case batchFunc:
return r.BatchOrders.Wait(ctx)
case withdrawFunc:
return r.WithdrawRequest.Wait(ctx)
case newReportFunc:
return r.CreateNewReport.Wait(ctx)
default:
return r.UnAuth.Wait(ctx)
}
}
// SetRateLimit returns the rate limit for the exchange
func SetRateLimit() *RateLimit {
return &RateLimit{
Auth: request.NewRateLimit(btcmarketsRateInterval, btcmarketsAuthLimit),
UnAuth: request.NewRateLimit(btcmarketsRateInterval, btcmarketsUnauthLimit),
OrderPlacement: request.NewRateLimit(btcmarketsRateInterval, btcmarketsOrderLimit),
BatchOrders: request.NewRateLimit(btcmarketsRateInterval, btcmarketsBatchOrderLimit),
WithdrawRequest: request.NewRateLimit(btcmarketsRateInterval, btcmarketsWithdrawLimit),
CreateNewReport: request.NewRateLimit(btcmarketsRateInterval, btcmarketsCreateNewReportLimit),
}
}