From 0cc2b9f5f3ac1afb4dcf04475213c146280cc760 Mon Sep 17 00:00:00 2001 From: Mike Keller Date: Thu, 17 Oct 2024 11:45:12 -0700 Subject: [PATCH 1/2] Track Reddit API rate-limits --- reddit.go | 19 +++++++++++++++++++ reddit_struct.go | 17 ++++++++++------- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/reddit.go b/reddit.go index 93089eb..bc8d39d 100644 --- a/reddit.go +++ b/reddit.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "strconv" "strings" "github.com/thecsw/mira/v4/models" @@ -38,6 +39,24 @@ func (c *Reddit) MiraRequest(method string, target string, payload map[string]st return nil, err } defer response.Body.Close() + + // Extract rate-limiting information from the response headers + if rateLimitUsed := response.Header.Get("X-Ratelimit-Used"); rateLimitUsed != "" { + if used, err := strconv.Atoi(rateLimitUsed); err == nil { + c.RateLimitUsed = used + } + } + if rateLimitRemaining := response.Header.Get("X-Ratelimit-Remaining"); rateLimitRemaining != "" { + if remaining, err := strconv.Atoi(rateLimitRemaining); err == nil { + c.RateLimitRemaining = remaining + } + } + if rateLimitReset := response.Header.Get("X-Ratelimit-Reset"); rateLimitReset != "" { + if reset, err := strconv.Atoi(rateLimitReset); err == nil { + c.RateLimitReset = reset + } + } + buf := new(bytes.Buffer) buf.ReadFrom(response.Body) data := buf.Bytes() diff --git a/reddit_struct.go b/reddit_struct.go index 9754b94..5caa601 100644 --- a/reddit_struct.go +++ b/reddit_struct.go @@ -8,13 +8,16 @@ import ( // Reddit is the main mira struct that practically // does everything type Reddit struct { - Token string `json:"access_token"` - Duration float64 `json:"expires_in"` - Creds Credentials - Chain chan *ChainVals - Stream Streaming - Values RedditVals - Client *http.Client + Token string `json:"access_token"` + Duration float64 `json:"expires_in"` + Creds Credentials + Chain chan *ChainVals + Stream Streaming + Values RedditVals + Client *http.Client + RateLimitUsed int // The number of requests used in the current rate limit window + RateLimitRemaining int // The number of requests left to use in the current rate limit window + RateLimitReset int // The number of seconds left in the current rate limit window } // Streaming is used for some durations on how frequently From 885b5f670d23519cb04aa75f0da51e36e255b498 Mon Sep 17 00:00:00 2001 From: Mike Keller Date: Thu, 17 Oct 2024 12:48:15 -0700 Subject: [PATCH 2/2] Fix parsing of X-Ratelimit-Remaining --- reddit.go | 4 ++-- reddit_struct.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/reddit.go b/reddit.go index bc8d39d..f388b0d 100644 --- a/reddit.go +++ b/reddit.go @@ -40,14 +40,14 @@ func (c *Reddit) MiraRequest(method string, target string, payload map[string]st } defer response.Body.Close() - // Extract rate-limiting information from the response headers + // Reddit returns integers for X-Ratelimit-Used and X-Ratelimit-Reset, but not for X-Ratelimit-Remaining if rateLimitUsed := response.Header.Get("X-Ratelimit-Used"); rateLimitUsed != "" { if used, err := strconv.Atoi(rateLimitUsed); err == nil { c.RateLimitUsed = used } } if rateLimitRemaining := response.Header.Get("X-Ratelimit-Remaining"); rateLimitRemaining != "" { - if remaining, err := strconv.Atoi(rateLimitRemaining); err == nil { + if remaining, err := strconv.ParseFloat(rateLimitRemaining, 64); err == nil { c.RateLimitRemaining = remaining } } diff --git a/reddit_struct.go b/reddit_struct.go index 5caa601..e0ad48d 100644 --- a/reddit_struct.go +++ b/reddit_struct.go @@ -15,9 +15,9 @@ type Reddit struct { Stream Streaming Values RedditVals Client *http.Client - RateLimitUsed int // The number of requests used in the current rate limit window - RateLimitRemaining int // The number of requests left to use in the current rate limit window - RateLimitReset int // The number of seconds left in the current rate limit window + RateLimitUsed int // The number of requests used in the current rate limit window + RateLimitRemaining float64 // The number of requests left to use in the current rate limit window + RateLimitReset int // The number of seconds left in the current rate limit window } // Streaming is used for some durations on how frequently