forked from thrasher-corp/gocryptotrader
/
ratelimit.go
51 lines (44 loc) · 1.2 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
package hitbtc
import (
"errors"
"time"
"github.com/nbltrust/gocryptotrader/exchanges/request"
"golang.org/x/time/rate"
)
const (
hitbtcRateInterval = time.Second
hitbtcMarketDataReqRate = 100
hitbtcTradingReqRate = 300
hitbtcAllOthers = 10
marketRequests request.EndpointLimit = iota
tradingRequests
otherRequests
)
// RateLimit implements the request.Limiter interface
type RateLimit struct {
MarketData *rate.Limiter
Trading *rate.Limiter
Other *rate.Limiter
}
// Limit limits outbound requests
func (r *RateLimit) Limit(f request.EndpointLimit) error {
switch f {
case marketRequests:
time.Sleep(r.MarketData.Reserve().Delay())
case tradingRequests:
time.Sleep(r.Trading.Reserve().Delay())
case otherRequests:
time.Sleep(r.Other.Reserve().Delay())
default:
return errors.New("functionality not found")
}
return nil
}
// SetRateLimit returns the rate limit for the exchange
func SetRateLimit() *RateLimit {
return &RateLimit{
MarketData: request.NewRateLimit(hitbtcRateInterval, hitbtcMarketDataReqRate),
Trading: request.NewRateLimit(hitbtcRateInterval, hitbtcTradingReqRate),
Other: request.NewRateLimit(hitbtcRateInterval, hitbtcAllOthers),
}
}