-
Notifications
You must be signed in to change notification settings - Fork 13
Track Reddit API rate-limits #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
|
|
||
| // 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to give these some default values other than 0? i.e., is 0 a valid value in regular runtime and could we have something else to signify its "uninitialized" and/or "unknown" (say, whenever |
||
| } | ||
| } | ||
| if rateLimitRemaining := response.Header.Get("X-Ratelimit-Remaining"); rateLimitRemaining != "" { | ||
| if remaining, err := strconv.ParseFloat(rateLimitRemaining, 64); 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() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the godoc and LSPs to work nicely, can you put the inlined comments above the declaration in godoc format of |
||
| 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 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link the same link from the PR? So whenever someone looks at the code in the future, they can see where these values are documented and look back on them—say when/if they change in the future.