@@ -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 .
13851385func 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.
14251426func 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.
14891556func TestDo_rateLimit_abuseRateLimitError (t * testing.T ) {
0 commit comments