/* Copyright © 2023-2025 Philipp Wolfer Scotty is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Scotty is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Scotty. If not, see . */ // Helper functions to set up rate limiting with resty. package ratelimit import ( "net/http" "strconv" "time" "github.com/go-resty/resty/v2" ) const ( RetryCount = 5 DefaultRateLimitWait = 5 * time.Second MaxWaitTime = 60 * time.Second ) // Implements rate HTTP header based limiting for resty. // // This works with servers that return the status code 429 (Too Many Requests) // and an HTTP header indicating the time in seconds until rate limit resets. // Common headers used are "X-RateLimit-Reset-In" or "Retry-After". // // Usage: // // ratelimit.EnableHTTPHeaderRateLimit(client, "Retry-After") func EnableHTTPHeaderRateLimit(client *resty.Client, resetInHeader string) { client.SetRetryCount(RetryCount) client.AddRetryCondition( func(r *resty.Response, err error) bool { code := r.StatusCode() return code == http.StatusTooManyRequests || code >= http.StatusInternalServerError }, ) client.SetRetryMaxWaitTime(MaxWaitTime) client.SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) { retryAfter := DefaultRateLimitWait if resp.StatusCode() == http.StatusTooManyRequests { retryAfterHeader, err := strconv.Atoi(resp.Header().Get(resetInHeader)) if err == nil { retryAfter = time.Duration(retryAfterHeader) * time.Second } } return retryAfter, nil }) }