Skip to content

Track Reddit API rate-limits#18

Open
mkell43 wants to merge 2 commits into
thecsw:masterfrom
mkell43:ratelimits
Open

Track Reddit API rate-limits#18
mkell43 wants to merge 2 commits into
thecsw:masterfrom
mkell43:ratelimits

Conversation

@mkell43

@mkell43 mkell43 commented Oct 17, 2024

Copy link
Copy Markdown

This should help those who wish to avoid sending unnecessary requests when close to or hitting the Reddit API rate limit. I used the headers Reddit documents here: Reddit API Wiki. The code handles missing or changed headers gracefully, so there isn't a risk of breaking if Reddit changes header names in the future.

@thecsw

thecsw commented Oct 18, 2024

Copy link
Copy Markdown
Owner

Hi and thanks for the PR! Yeah, this project was built way before all of the AI craze and companies locking down their content more aggressively behind API limits. I'll leave very minor suggestions, but the ability to do this is definitely needed.

@thecsw thecsw left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small requests around to improve the maintenance—could you also add an example of running this code? It can go to /examples with possibly a small snippet from running live, showing the expected behavior.

The main reason for my caution is that I haven't worked with Reddit for many years and all of this web code can't really be unit-tested, so it's nice having a proof that "some point in time this worked."

Thanks!

Comment thread reddit_struct.go
Stream Streaming
Values RedditVals
Client *http.Client
RateLimitUsed int // The number of requests used in the current rate limit window

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 // RateLimitUsed indicates the number of...

Comment thread reddit.go
}
defer response.Body.Close()

// Reddit returns integers for X-Ratelimit-Used and X-Ratelimit-Reset, but not for X-Ratelimit-Remaining

Copy link
Copy Markdown
Owner

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.

Comment thread reddit.go
// 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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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 err != nil)? Since all of these are signed, -1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants