forked from timpalpant/go-tradier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate_limit.go
42 lines (34 loc) · 915 Bytes
/
rate_limit.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
package tradier
import (
"net/http"
"strconv"
"strings"
"time"
)
// Extract quota violation expiration from body message.
func parseQuotaViolationExpiration(body string) time.Time {
if !strings.HasPrefix(body, "Quota Violation") {
return time.Time{}
}
parts := strings.Fields(body)
ms, err := strconv.ParseInt(parts[len(parts)-1], 10, 64)
if err != nil {
return time.Time{}
}
return time.Unix(ms/1000, 0)
}
// Attempt to extract the rate-limit expiry time if we have exceeded the rate limit available.
func getRateLimitExpiration(h http.Header) time.Time {
rateLimit, err := strconv.ParseInt(h.Get(rateLimitAvailable), 10, 64)
if err != nil {
return time.Time{}
}
rateLimitExpiry, err := strconv.ParseInt(h.Get(rateLimitExpiry), 10, 64)
if err != nil {
return time.Time{}
}
if rateLimit == 0 && rateLimitExpiry > 0 {
return time.Unix(rateLimitExpiry, 0)
}
return time.Time{}
}