more info on ratelimit; change useragent

This commit is contained in:
Andre Medeiros 2022-03-12 14:22:00 -05:00
parent 7bff205523
commit 195bd55426
2 changed files with 8 additions and 4 deletions

View file

@ -20,8 +20,9 @@ const (
SkipRateLimiting = "<SKIP_RATE_LIMITING>" SkipRateLimiting = "<SKIP_RATE_LIMITING>"
RequestRemainingBuffer = 50 RequestRemainingBuffer = 50
RateLimitRemainingHeader = "X-Ratelimit-Remaining" RateLimitRemainingHeader = "x-ratelimit-remaining"
RateLimitResetHeader = "X-Ratelimit-Reset" RateLimitUsedHeader = "x-ratelimit-used"
RateLimitResetHeader = "x-ratelimit-reset"
) )
type Client struct { type Client struct {
@ -36,6 +37,7 @@ type Client struct {
type RateLimitingInfo struct { type RateLimitingInfo struct {
Remaining float64 Remaining float64
Used int
Reset int Reset int
Present bool Present bool
} }
@ -150,6 +152,7 @@ func (rc *Client) doRequest(r *Request) ([]byte, *RateLimitingInfo, error) {
if resp.Header.Get(RateLimitRemainingHeader) != "" { if resp.Header.Get(RateLimitRemainingHeader) != "" {
rli.Present = true rli.Present = true
rli.Remaining, _ = strconv.ParseFloat(resp.Header.Get(RateLimitRemainingHeader), 64) rli.Remaining, _ = strconv.ParseFloat(resp.Header.Get(RateLimitRemainingHeader), 64)
rli.Used, _ = strconv.Atoi(resp.Header.Get(RateLimitUsedHeader))
rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader)) rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader))
} }
@ -243,7 +246,8 @@ func (rac *AuthenticatedClient) markRateLimited(rli *RateLimitingInfo) error {
key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId) key := fmt.Sprintf("reddit:%s:ratelimited", rac.redditId)
duration := time.Duration(rli.Reset) * time.Second duration := time.Duration(rli.Reset) * time.Second
_, err := rac.redis.SetEX(context.Background(), key, rli.Remaining, duration).Result() info := fmt.Sprintf("%+v", *rli)
_, err := rac.redis.SetEX(context.Background(), key, info, duration).Result()
return err return err
} }

View file

@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
const userAgent = "server:apollo-backend:v1.0 (by /u/iamthatis)" const userAgent = "server:apollo-backend:v1.0 (by /u/iamthatis) contact me@christianselig.com"
type Request struct { type Request struct {
body url.Values body url.Values