HTTPCLIENT-2020: DefaultBackoffStrategy should include TOO_MANY_REQUESTS (429) too

This commit is contained in:
Michael Osipov 2019-11-27 13:16:44 +01:00 committed by GitHub
parent 9552d5dd1d
commit 742bc47a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 4 deletions

View File

@ -37,7 +37,7 @@ import org.apache.hc.core5.http.HttpStatus;
/** /**
* This {@link ConnectionBackoffStrategy} backs off either for a raw * This {@link ConnectionBackoffStrategy} backs off either for a raw
* network socket or connection timeout or if the server explicitly * network socket or connection timeout or if the server explicitly
* sends a 503 (Service Unavailable) response. * sends a 429 (Too Many Requests) or a 503 (Service Unavailable) response.
* *
* @since 4.2 * @since 4.2
*/ */
@ -51,7 +51,8 @@ public class DefaultBackoffStrategy implements ConnectionBackoffStrategy {
@Override @Override
public boolean shouldBackoff(final HttpResponse resp) { public boolean shouldBackoff(final HttpResponse resp) {
return resp.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE; return resp.getCode() == HttpStatus.SC_TOO_MANY_REQUESTS ||
resp.getCode() == HttpStatus.SC_SERVICE_UNAVAILABLE;
} }
} }

View File

@ -71,9 +71,15 @@ public class TestDefaultBackoffStrategy {
} }
@Test @Test
public void doesNotBackOffForNon503StatusCodes() { public void backsOffForTooManyRequests() {
final HttpResponse resp = new BasicHttpResponse(HttpStatus.SC_TOO_MANY_REQUESTS, "Too Many Requests");
assertTrue(impl.shouldBackoff(resp));
}
@Test
public void doesNotBackOffForNon429And503StatusCodes() {
for(int i = 100; i <= 599; i++) { for(int i = 100; i <= 599; i++) {
if (i == HttpStatus.SC_SERVICE_UNAVAILABLE) { if (i== HttpStatus.SC_TOO_MANY_REQUESTS || i == HttpStatus.SC_SERVICE_UNAVAILABLE) {
continue; continue;
} }
final HttpResponse resp = new BasicHttpResponse(i, "Foo"); final HttpResponse resp = new BasicHttpResponse(i, "Foo");