-
-
Notifications
You must be signed in to change notification settings - Fork 289
Disabling automatic retry when Spotify returns 429 #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dargmuesli
merged 4 commits into
spotify-web-api-java:develop
from
SergioBarbero:tooManyRequestTest
Oct 5, 2023
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e0c3b6b
ISSUE-344: configuring http client to not retry when Spotify returns 429
SergioBarbero 2b2fb4e
chore(deps): update wiremock
dargmuesli 803a83f
feat(http-client): use custom request retry strategy
dargmuesli 7683a95
test(http-request-retry-strategy): verify functionality
dargmuesli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
src/main/java/se/michaelthelin/spotify/SpotifyHttpRequestRetryStrategy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
|
|
||
| package se.michaelthelin.spotify; | ||
|
|
||
| import org.apache.hc.client5.http.HttpRequestRetryStrategy; | ||
| import org.apache.hc.core5.annotation.Contract; | ||
| import org.apache.hc.core5.annotation.ThreadingBehavior; | ||
| import org.apache.hc.core5.concurrent.CancellableDependency; | ||
| import org.apache.hc.core5.http.*; | ||
| import org.apache.hc.core5.http.protocol.HttpContext; | ||
| import org.apache.hc.core5.util.Args; | ||
| import org.apache.hc.core5.util.TimeValue; | ||
|
|
||
| import javax.net.ssl.SSLException; | ||
| import java.io.IOException; | ||
| import java.io.InterruptedIOException; | ||
| import java.net.ConnectException; | ||
| import java.net.NoRouteToHostException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.*; | ||
|
|
||
| /** | ||
| * Default implementation of the {@link HttpRequestRetryStrategy} interface. | ||
| * | ||
| * @since 5.0 | ||
| */ | ||
| @Contract(threading = ThreadingBehavior.STATELESS) | ||
| public class SpotifyHttpRequestRetryStrategy implements HttpRequestRetryStrategy { | ||
|
|
||
| /** | ||
| * Maximum number of allowed retries | ||
| */ | ||
| private final int maxRetries; | ||
|
|
||
| /** | ||
| * Retry interval between subsequent retries | ||
| */ | ||
| private final TimeValue defaultRetryInterval; | ||
|
|
||
| /** | ||
| * Derived {@code IOExceptions} which shall not be retried | ||
| */ | ||
| private final Set<Class<? extends IOException>> nonRetriableIOExceptionClasses; | ||
|
|
||
| /** | ||
| * HTTP status codes which shall be retried | ||
| */ | ||
| private final Set<Integer> retriableCodes; | ||
|
|
||
| protected SpotifyHttpRequestRetryStrategy( | ||
| final int maxRetries, | ||
| final TimeValue defaultRetryInterval, | ||
| final Collection<Class<? extends IOException>> clazzes, | ||
| final Collection<Integer> codes) { | ||
| Args.notNegative(maxRetries, "maxRetries"); | ||
| Args.notNegative(defaultRetryInterval.getDuration(), "defaultRetryInterval"); | ||
| this.maxRetries = maxRetries; | ||
| this.defaultRetryInterval = defaultRetryInterval; | ||
| this.nonRetriableIOExceptionClasses = new HashSet<>(clazzes); | ||
| this.retriableCodes = new HashSet<>(codes); | ||
| } | ||
|
|
||
| /** | ||
| * Create the HTTP request retry strategy using the following list of | ||
| * non-retriable I/O exception classes:<br> | ||
| * <ul> | ||
| * <li>InterruptedIOException</li> | ||
| * <li>UnknownHostException</li> | ||
| * <li>ConnectException</li> | ||
| * <li>ConnectionClosedException</li> | ||
| * <li>NoRouteToHostException</li> | ||
| * <li>SSLException</li> | ||
| * </ul> | ||
| * <p> | ||
| * and retriable HTTP status codes:<br> | ||
| * <ul> | ||
| * <li>SC_SERVICE_UNAVAILABLE (503)</li> | ||
| * </ul> | ||
| * | ||
| * @param maxRetries how many times to retry; 0 means no retries | ||
| * @param defaultRetryInterval the default retry interval between | ||
| * subsequent retries if the {@code Retry-After} header is not set | ||
| * or invalid. | ||
| */ | ||
| public SpotifyHttpRequestRetryStrategy( | ||
| final int maxRetries, | ||
| final TimeValue defaultRetryInterval) { | ||
| this(maxRetries, defaultRetryInterval, | ||
| Arrays.asList( | ||
| InterruptedIOException.class, | ||
| UnknownHostException.class, | ||
| ConnectException.class, | ||
| ConnectionClosedException.class, | ||
| NoRouteToHostException.class, | ||
| SSLException.class), | ||
| List.of( | ||
| HttpStatus.SC_SERVICE_UNAVAILABLE)); | ||
| } | ||
|
|
||
| /** | ||
| * Create the HTTP request retry strategy with a max retry count of 1, | ||
| * default retry interval of 1 second, and using the following list of | ||
| * non-retriable I/O exception classes:<br> | ||
| * <ul> | ||
| * <li>InterruptedIOException</li> | ||
| * <li>UnknownHostException</li> | ||
| * <li>ConnectException</li> | ||
| * <li>ConnectionClosedException</li> | ||
| * <li>SSLException</li> | ||
| * </ul> | ||
| * <p> | ||
| * and retriable HTTP status codes:<br> | ||
| * <ul> | ||
| * <li>SC_SERVICE_UNAVAILABLE (503)</li> | ||
| * </ul> | ||
| */ | ||
| public SpotifyHttpRequestRetryStrategy() { | ||
| this(1, TimeValue.ofSeconds(1L)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean retryRequest( | ||
| final HttpRequest request, | ||
| final IOException exception, | ||
| final int execCount, | ||
| final HttpContext context) { | ||
| Args.notNull(request, "request"); | ||
| Args.notNull(exception, "exception"); | ||
|
|
||
| if (execCount > this.maxRetries) { | ||
| // Do not retry if over max retries | ||
| return false; | ||
| } | ||
| if (this.nonRetriableIOExceptionClasses.contains(exception.getClass())) { | ||
| return false; | ||
| } else { | ||
| for (final Class<? extends IOException> rejectException : this.nonRetriableIOExceptionClasses) { | ||
| if (rejectException.isInstance(exception)) { | ||
| return false; | ||
| } | ||
| } | ||
| } | ||
| if (request instanceof CancellableDependency && ((CancellableDependency) request).isCancelled()) { | ||
| return false; | ||
| } | ||
|
|
||
| // Retry if the request is considered idempotent | ||
| return handleAsIdempotent(request); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean retryRequest( | ||
| final HttpResponse response, | ||
| final int execCount, | ||
| final HttpContext context) { | ||
| Args.notNull(response, "response"); | ||
|
|
||
| return execCount <= this.maxRetries && retriableCodes.contains(response.getCode()); | ||
| } | ||
|
|
||
| @Override | ||
| public TimeValue getRetryInterval( | ||
dargmuesli marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| final HttpResponse response, | ||
| final int execCount, | ||
| final HttpContext context) { | ||
| Args.notNull(response, "response"); | ||
|
|
||
| return this.defaultRetryInterval; | ||
| } | ||
|
|
||
| protected boolean handleAsIdempotent(final HttpRequest request) { | ||
| return Method.isIdempotent(request.getMethod()); | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.