mirror of
https://github.com/christianselig/apollo-backend
synced 2024-11-10 22:17:44 +00:00
record timestamp and crazy ratelimit cases
This commit is contained in:
parent
b9389511f6
commit
3bf38b0630
1 changed files with 10 additions and 0 deletions
|
@ -40,6 +40,7 @@ type RateLimitingInfo struct {
|
||||||
Used int
|
Used int
|
||||||
Reset int
|
Reset int
|
||||||
Present bool
|
Present bool
|
||||||
|
Timestamp string
|
||||||
}
|
}
|
||||||
|
|
||||||
var backoffSchedule = []time.Duration{
|
var backoffSchedule = []time.Duration{
|
||||||
|
@ -154,6 +155,7 @@ func (rc *Client) doRequest(r *Request) ([]byte, *RateLimitingInfo, error) {
|
||||||
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.Used, _ = strconv.Atoi(resp.Header.Get(RateLimitUsedHeader))
|
||||||
rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader))
|
rli.Reset, _ = strconv.Atoi(resp.Header.Get(RateLimitResetHeader))
|
||||||
|
rli.Timestamp = time.Now().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.StatusCode != 200 {
|
if resp.StatusCode != 200 {
|
||||||
|
@ -257,6 +259,14 @@ 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
|
||||||
info := fmt.Sprintf("%+v", *rli)
|
info := fmt.Sprintf("%+v", *rli)
|
||||||
|
|
||||||
|
if rli.Used > 2000 {
|
||||||
|
_, err := rac.redis.HSet(context.Background(), "reddit:ratelimited:crazy", rac.redditId, info).Result()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
_, err := rac.redis.SetEX(context.Background(), key, info, duration).Result()
|
_, err := rac.redis.SetEX(context.Background(), key, info, duration).Result()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue