Skip to content

Commit

Permalink
Don't update the ratelimits if we got a response from a cache
Browse files Browse the repository at this point in the history
The recommended cache, https://github.com/gregjones/httpcache, returns
an X-From-Cache: 1 header whenever it serves a response from the cache.

Cached responses might include (possibly stale) ratelimit headers, which
we would've already processed in the past.
  • Loading branch information
Jille committed Jan 26, 2022
1 parent 51df45c commit 155f27f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions github/github.go
Expand Up @@ -653,9 +653,13 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro

response := newResponse(resp)

c.rateMu.Lock()
c.rateLimits[rateLimitCategory] = response.Rate
c.rateMu.Unlock()
// Don't update the rate limits if this was a cached response.
// X-From-Cache is set by https://github.com/gregjones/httpcache
if response.Header.Get("X-From-Cache") == "" {
c.rateMu.Lock()
c.rateLimits[rateLimitCategory] = response.Rate
c.rateMu.Unlock()
}

err = CheckResponse(resp)
if err != nil {
Expand Down

0 comments on commit 155f27f

Please sign in to comment.