forked from thrasher-corp/gocryptotrader
/
ratelimit.go
38 lines (32 loc) · 816 Bytes
/
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
package bitmex
import (
"context"
"time"
"github.com/idoall/gocryptotrader/exchanges/request"
"golang.org/x/time/rate"
)
// Bitmex rate limits
const (
bitmexRateInterval = time.Minute
bitmexUnauthRate = 30
bitmexAuthRate = 60
)
// RateLimit implements the request.Limiter interface
type RateLimit struct {
Auth *rate.Limiter
UnAuth *rate.Limiter
}
// Limit limits outbound calls
func (r *RateLimit) Limit(ctx context.Context, f request.EndpointLimit) error {
if f == request.Auth {
return r.Auth.Wait(ctx)
}
return r.UnAuth.Wait(ctx)
}
// SetRateLimit returns the rate limit for the exchange
func SetRateLimit() *RateLimit {
return &RateLimit{
Auth: request.NewRateLimit(bitmexRateInterval, bitmexAuthRate),
UnAuth: request.NewRateLimit(bitmexRateInterval, bitmexUnauthRate),
}
}