Skip to content

Commit 22eba4c

Browse files
committed
feat: Add context cancellation
1 parent 41de545 commit 22eba4c

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

‎github/github.go‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,10 @@ func (c *Client) BareDo(ctx context.Context, req *http.Request) (*Response, erro
892892

893893
rateLimitError, ok := err.(*RateLimitError)
894894
if ok && req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
895-
sleepUntilResetWithBuffer(rateLimitError.Rate.Reset.Time)
895+
err := sleepUntilResetWithBuffer(req.Context(), rateLimitError.Rate.Reset.Time)
896+
if err != nil {
897+
return response, err
898+
}
896899
// retry the request once when the rate limit has reset
897900
return c.BareDo(context.WithValue(req.Context(), SleepUntilPrimaryRateLimitResetWhenRateLimited, nil), req)
898901
}
@@ -950,10 +953,6 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory Rat
950953
rate := c.rateLimits[rateLimitCategory]
951954
c.rateMu.Unlock()
952955
if !rate.Reset.Time.IsZero() && rate.Remaining == 0 && time.Now().Before(rate.Reset.Time) {
953-
if req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
954-
sleepUntilResetWithBuffer(rate.Reset.Time)
955-
return nil
956-
}
957956
// Create a fake response.
958957
resp := &http.Response{
959958
Status: http.StatusText(http.StatusForbidden),
@@ -962,6 +961,19 @@ func (c *Client) checkRateLimitBeforeDo(req *http.Request, rateLimitCategory Rat
962961
Header: make(http.Header),
963962
Body: io.NopCloser(strings.NewReader("")),
964963
}
964+
965+
if req.Context().Value(SleepUntilPrimaryRateLimitResetWhenRateLimited) != nil {
966+
err := sleepUntilResetWithBuffer(req.Context(), rate.Reset.Time)
967+
if err == nil {
968+
return nil
969+
}
970+
return &RateLimitError{
971+
Rate: rate,
972+
Response: resp,
973+
Message: fmt.Sprintf("Context cancelled while waiting for rate limit to reset until %v, not making remote request.", rate.Reset.Time),
974+
}
975+
}
976+
965977
return &RateLimitError{
966978
Rate: rate,
967979
Response: resp,
@@ -1526,9 +1538,18 @@ func formatRateReset(d time.Duration) string {
15261538
return fmt.Sprintf("[rate reset in %v]", timeString)
15271539
}
15281540

1529-
func sleepUntilResetWithBuffer(reset time.Time) {
1541+
func sleepUntilResetWithBuffer(ctx context.Context, reset time.Time) error {
15301542
buffer := time.Second
1531-
time.Sleep(time.Until(reset) + buffer)
1543+
timer := time.NewTimer(time.Until(reset) + buffer)
1544+
select {
1545+
case <-ctx.Done():
1546+
if !timer.Stop() {
1547+
<-timer.C
1548+
}
1549+
return ctx.Err()
1550+
case <-timer.C:
1551+
}
1552+
return nil
15321553
}
15331554

15341555
// When using roundTripWithOptionalFollowRedirect, note that it

‎github/github_test.go‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,7 @@ func TestDo_rateLimit_ignoredFromCache(t *testing.T) {
13811381
}
13821382
}
13831383

1384-
// Ensure *RateLimitError is returned when API rate limit is exceeded.
1384+
// Ensure sleeps until the rate limit is reset when the client is rate limited.
13851385
func TestDo_rateLimit_sleepUntilResponseResetLimit(t *testing.T) {
13861386
client, mux, _, teardown := setup()
13871387
defer teardown()
@@ -1422,6 +1422,7 @@ func TestDo_rateLimit_sleepUntilResponseResetLimit(t *testing.T) {
14221422
}
14231423
}
14241424

1425+
// Ensure tries to sleep until the rate limit is reset when the client is rate limited, but only once.
14251426
func TestDo_rateLimit_sleepUntilResponseResetLimitRetryOnce(t *testing.T) {
14261427
client, mux, _, teardown := setup()
14271428
defer teardown()
@@ -1484,6 +1485,72 @@ func TestDo_rateLimit_sleepUntilClientResetLimit(t *testing.T) {
14841485
}
14851486
}
14861487

1488+
// Ensure sleep is aborted when the context is cancelled.
1489+
func TestDo_rateLimit_abortSleepContextCancelled(t *testing.T) {
1490+
client, mux, _, teardown := setup()
1491+
defer teardown()
1492+
1493+
// We use a 1 minute reset time to ensure the sleep is not completed.
1494+
reset := time.Now().UTC().Add(time.Minute)
1495+
requestCount := 0
1496+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1497+
requestCount++
1498+
w.Header().Set(headerRateLimit, "60")
1499+
w.Header().Set(headerRateRemaining, "0")
1500+
w.Header().Set(headerRateReset, fmt.Sprint(reset.Unix()))
1501+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
1502+
w.WriteHeader(http.StatusForbidden)
1503+
fmt.Fprintln(w, `{
1504+
"message": "API rate limit exceeded for xxx.xxx.xxx.xxx. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
1505+
"documentation_url": "https://docs.github.com/en/rest/overview/resources-in-the-rest-api#abuse-rate-limits"
1506+
}`)
1507+
})
1508+
1509+
req, _ := client.NewRequest("GET", ".", nil)
1510+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
1511+
defer cancel()
1512+
_, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
1513+
if !errors.Is(err, context.DeadlineExceeded) {
1514+
t.Error("Expected context deadline exceeded error.")
1515+
}
1516+
if got, want := requestCount, 1; got != want {
1517+
t.Errorf("Expected 1 requests, got %d", got)
1518+
}
1519+
}
1520+
1521+
// Ensure sleep is aborted when the context is cancelled on initial request.
1522+
func TestDo_rateLimit_abortSleepContextCancelledClientLimit(t *testing.T) {
1523+
client, mux, _, teardown := setup()
1524+
defer teardown()
1525+
1526+
reset := time.Now().UTC().Add(time.Minute)
1527+
client.rateLimits[CoreCategory] = Rate{Limit: 5000, Remaining: 0, Reset: Timestamp{reset}}
1528+
requestCount := 0
1529+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
1530+
requestCount++
1531+
w.Header().Set(headerRateLimit, "5000")
1532+
w.Header().Set(headerRateRemaining, "5000")
1533+
w.Header().Set(headerRateReset, fmt.Sprint(reset.Add(time.Hour).Unix()))
1534+
w.Header().Set("Content-Type", "application/json; charset=utf-8")
1535+
w.WriteHeader(http.StatusOK)
1536+
fmt.Fprintln(w, `{}`)
1537+
})
1538+
req, _ := client.NewRequest("GET", ".", nil)
1539+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
1540+
defer cancel()
1541+
_, err := client.Do(context.WithValue(ctx, SleepUntilPrimaryRateLimitResetWhenRateLimited, true), req, nil)
1542+
rateLimitError, ok := err.(*RateLimitError)
1543+
if !ok {
1544+
t.Fatalf("Expected a *rateLimitError error; got %#v.", err)
1545+
}
1546+
if got, wantSuffix := rateLimitError.Message, "Context cancelled while waiting for rate limit to reset until"; !strings.HasPrefix(got, wantSuffix) {
1547+
t.Errorf("Expected request to be prevented because context cancellation, got: %v.", got)
1548+
}
1549+
if got, want := requestCount, 0; got != want {
1550+
t.Errorf("Expected 1 requests, got %d", got)
1551+
}
1552+
}
1553+
14871554
// Ensure *AbuseRateLimitError is returned when the response indicates that
14881555
// the client has triggered an abuse detection mechanism.
14891556
func TestDo_rateLimit_abuseRateLimitError(t *testing.T) {

0 commit comments

Comments
 (0)