forked from beldur/kraken-go-api-client
/
rate_limiter.go
116 lines (96 loc) · 2 KB
/
rate_limiter.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package krakenapi
import (
"log"
"net/url"
"sync"
"time"
)
type call struct {
api *KrakenApi
reqURL string
values url.Values
headers map[string]string
typ interface{}
wg sync.WaitGroup
res interface{}
err error
}
func newCall(api *KrakenApi, reqURL string, values url.Values, headers map[string]string, typ interface{}) *call {
c := &call{
api: api,
reqURL: reqURL,
values: values,
headers: headers,
typ: typ,
}
c.lock()
return c
}
func (c *call) lock() {
c.wg.Add(1)
}
func (c *call) done(res interface{}, err error) {
c.res = res
c.err = err
c.wg.Done()
}
func (c *call) waitFor() (interface{}, error) {
c.wg.Wait()
return c.res, c.err
}
type rateLimiter struct {
sentTime time.Time
callList chan *call
extraWait bool
}
func NewPublicRateLimit() *rateLimiter {
rl := &rateLimiter{
callList: make(chan *call, 100),
}
go rl.startSender()
return rl
}
func (r *rateLimiter) limitedRequest(api *KrakenApi, reqURL string, values url.Values, headers map[string]string, typ interface{}) (interface{}, error) {
call := newCall(api, reqURL, values, headers, typ)
r.callList <- call
resp, err := call.waitFor()
return resp, err
}
func (r *rateLimiter) startSender() {
for i := 0; ; i++ {
c := <-r.callList
if r.extraWait {
time.Sleep(time.Second * 10)
r.extraWait = false
}
r.sentTime = time.Now()
go r.doRequest(c)
}
}
func (r *rateLimiter) channelIsFree() bool {
if r.extraWait {
return false
}
return time.Since(r.sentTime) > time.Second
}
func (r *rateLimiter) waitForChannel() {
diff := time.Second - time.Since(r.sentTime)
if r.extraWait {
diff = time.Second * 5
defer r.cleanExtraWait()
} else {
diff = time.Millisecond * 0
}
log.Printf("wait: %s", diff)
time.Sleep(diff)
}
func (r *rateLimiter) doRequest(c *call) {
resp, err := c.api.doRequest(c.reqURL, c.values, c.headers, c.typ)
if err != nil {
r.extraWait = true
}
c.done(resp, err)
}
func (r *rateLimiter) cleanExtraWait() {
r.extraWait = false
}