Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.2.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<packaging>jar</packaging>
Expand Down
26 changes: 20 additions & 6 deletions src/main/java/se/michaelthelin/spotify/SpotifyHttpManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package se.michaelthelin.spotify;

import com.google.gson.*;
import org.apache.hc.client5.http.HttpRequestRetryStrategy;
import org.apache.hc.client5.http.auth.AuthScope;
import org.apache.hc.client5.http.auth.UsernamePasswordCredentials;
import org.apache.hc.client5.http.cache.CacheResponseStatus;
Expand All @@ -9,6 +10,7 @@
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.classic.methods.HttpPut;
import org.apache.hc.client5.http.config.ConnectionConfig;
import org.apache.hc.client5.http.config.RequestConfig;
import org.apache.hc.client5.http.cookie.StandardCookieSpec;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
Expand All @@ -17,6 +19,7 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
import org.apache.hc.core5.http.*;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.util.Timeout;
Expand Down Expand Up @@ -72,34 +75,45 @@ public SpotifyHttpManager(Builder builder) {
);
}

ConnectionConfig connectionConfig = ConnectionConfig
.custom()
.setConnectTimeout(builder.connectTimeout != null
? Timeout.ofMilliseconds(builder.connectTimeout)
: ConnectionConfig.DEFAULT.getConnectTimeout())
.build();
BasicHttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
connectionManager.setConnectionConfig(connectionConfig);
RequestConfig requestConfig = RequestConfig
.custom()
.setCookieSpec(StandardCookieSpec.STRICT)
.setProxy(proxy)
.setConnectionRequestTimeout(builder.connectionRequestTimeout != null
? Timeout.ofMilliseconds(builder.connectionRequestTimeout)
: RequestConfig.DEFAULT.getConnectionRequestTimeout())
.setConnectTimeout(builder.connectTimeout != null
? Timeout.ofMilliseconds(builder.connectTimeout)
: RequestConfig.DEFAULT.getConnectTimeout())
.setResponseTimeout(builder.socketTimeout != null
? Timeout.ofMilliseconds(builder.socketTimeout)
: RequestConfig.DEFAULT.getResponseTimeout())
.build();
HttpRequestRetryStrategy retryStrategy = new SpotifyHttpRequestRetryStrategy();

this.httpClient = HttpClients
.custom()
.disableContentCompression()
.setConnectionManager(connectionManager)
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
.disableContentCompression()
.setProxy(proxy)
.setRetryStrategy(retryStrategy)
.build();

this.httpClientCaching = CachingHttpClients
.custom()
.setCacheConfig(cacheConfig)
.disableContentCompression()
.setConnectionManager(connectionManager)
.setDefaultCredentialsProvider(credentialsProvider)
.setDefaultRequestConfig(requestConfig)
.disableContentCompression()
.setProxy(proxy)
.setRetryStrategy(retryStrategy)
.build();
}

Expand Down
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(
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());
}

}
Loading