@@ -31,8 +31,10 @@ impl GithubClient {
31
31
. with_context ( || format ! ( "building reqwest {}" , req_dbg) ) ?;
32
32
33
33
let mut resp = self . client . execute ( req. try_clone ( ) . unwrap ( ) ) . await ?;
34
- if let Some ( sleep) = Self :: needs_retry ( & resp) . await {
35
- resp = self . retry ( req, sleep, MAX_ATTEMPTS ) . await ?;
34
+ if self . retry_rate_limit {
35
+ if let Some ( sleep) = Self :: needs_retry ( & resp) . await {
36
+ resp = self . retry ( req, sleep, MAX_ATTEMPTS ) . await ?;
37
+ }
36
38
}
37
39
let maybe_err = resp. error_for_status_ref ( ) . err ( ) ;
38
40
let body = resp
@@ -51,7 +53,10 @@ impl GithubClient {
51
53
const REMAINING : & str = "X-RateLimit-Remaining" ;
52
54
const RESET : & str = "X-RateLimit-Reset" ;
53
55
54
- if resp. status ( ) . is_success ( ) {
56
+ if !matches ! (
57
+ resp. status( ) ,
58
+ StatusCode :: FORBIDDEN | StatusCode :: TOO_MANY_REQUESTS
59
+ ) {
55
60
return None ;
56
61
}
57
62
@@ -60,12 +65,6 @@ impl GithubClient {
60
65
return None ;
61
66
}
62
67
63
- // Weird github api behavior. It asks us to retry but also has a remaining count above 1
64
- // Try again immediately and hope for the best...
65
- if headers[ REMAINING ] != "0" {
66
- return Some ( Duration :: from_secs ( 0 ) ) ;
67
- }
68
-
69
68
let reset_time = headers[ RESET ] . to_str ( ) . unwrap ( ) . parse :: < u64 > ( ) . unwrap ( ) ;
70
69
Some ( Duration :: from_secs ( Self :: calc_sleep ( reset_time) + 10 ) )
71
70
}
@@ -2149,6 +2148,8 @@ pub struct GithubClient {
2149
2148
api_url : String ,
2150
2149
graphql_url : String ,
2151
2150
raw_url : String ,
2151
+ /// If `true`, requests will sleep if it hits GitHub's rate limit.
2152
+ retry_rate_limit : bool ,
2152
2153
}
2153
2154
2154
2155
impl GithubClient {
@@ -2159,6 +2160,7 @@ impl GithubClient {
2159
2160
api_url,
2160
2161
graphql_url,
2161
2162
raw_url,
2163
+ retry_rate_limit : false ,
2162
2164
}
2163
2165
}
2164
2166
@@ -2174,6 +2176,14 @@ impl GithubClient {
2174
2176
)
2175
2177
}
2176
2178
2179
+ /// Sets whether or not this client will retry when it hits GitHub's rate limit.
2180
+ ///
2181
+ /// Just beware that the retry may take a long time (like 30 minutes,
2182
+ /// depending on various factors).
2183
+ pub fn set_retry_rate_limit ( & mut self , retry : bool ) {
2184
+ self . retry_rate_limit = retry;
2185
+ }
2186
+
2177
2187
pub fn raw ( & self ) -> & Client {
2178
2188
& self . client
2179
2189
}
0 commit comments