Skip to content

Commit

Permalink
Use more appropriate http timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
sebr72 committed Jan 24, 2025
1 parent 3ef4206 commit 57357fb
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -32,7 +31,6 @@
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.mapfish.print.config.Configuration;
import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
Expand All @@ -53,54 +51,43 @@ public class MfClientHttpRequestFactoryImpl extends HttpComponentsClientHttpRequ
*
* @param maxConnTotal Maximum total connections.
* @param maxConnPerRoute Maximum connections per route.
* @param connectionRequestTimeout Number of milliseconds used when requesting a connection from
* the connection manager.
* @param connectTimeout Number of milliseconds until a connection is established.
* @param socketTimeout Maximum number of milliseconds during which a socket remains inactive
* between two consecutive data packets.
*/
public MfClientHttpRequestFactoryImpl(
final int maxConnTotal,
final int maxConnPerRoute,
final ThreadPoolJobManager threadPoolJobManager) {
super(createHttpClient(maxConnTotal, maxConnPerRoute, threadPoolJobManager));
final int connectionRequestTimeout,
final int connectTimeout,
final int socketTimeout) {
super(
createHttpClient(
maxConnTotal,
maxConnPerRoute,
connectionRequestTimeout,
connectTimeout,
socketTimeout));
}

@Nullable
static Configuration getCurrentConfiguration() {
return CURRENT_CONFIGURATION.get();
}

/**
* Return the number of milliseconds until the timeout Use the Automatic cancellation timeout if
* not defined.
*
* @param name timeout idemtifier
* @return the number of milliseconds until the timeout
*/
private static int getTimeoutValue(
final String name, final ThreadPoolJobManager threadPoolJobManager) {
final String value = System.getProperty(name);
if (value == null) {
long millis = TimeUnit.SECONDS.toMillis(threadPoolJobManager.getTimeout());
if (millis > Integer.MAX_VALUE) {
LOGGER.warn(
"The value of {} is too large. The timeout will be set to the maximum value of {}",
name,
Integer.MAX_VALUE);
return Integer.MAX_VALUE;
} else {
return Integer.parseInt(Long.toString(millis));
}
}
return Integer.parseInt(value);
}

private static CloseableHttpClient createHttpClient(
final int maxConnTotal,
final int maxConnPerRoute,
final ThreadPoolJobManager threadPoolJobManager) {
final int connectionRequestTimeout,
final int connectTimeout,
final int socketTimeout) {
final RequestConfig requestConfig =
RequestConfig.custom()
.setConnectionRequestTimeout(
getTimeoutValue("http.connectionRequestTimeout", threadPoolJobManager))
.setConnectTimeout(getTimeoutValue("http.connectTimeout", threadPoolJobManager))
.setSocketTimeout(getTimeoutValue("http.socketTimeout", threadPoolJobManager))
.setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout)
.setSocketTimeout(socketTimeout)
.build();

final HttpClientBuilder httpClientBuilder =
Expand All @@ -118,9 +105,9 @@ private static CloseableHttpClient createHttpClient(
LOGGER.debug(
"Created CloseableHttpClient using connectionRequestTimeout: {} connectTimeout: {}"
+ " socketTimeout: {}",
getTimeoutValue("http.connectionRequestTimeout", threadPoolJobManager),
getTimeoutValue("http.connectTimeout", threadPoolJobManager),
getTimeoutValue("http.socketTimeout", threadPoolJobManager));
connectionRequestTimeout,
connectTimeout,
socketTimeout);
return closeableHttpClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
<bean id="httpClientFactory" class="org.mapfish.print.http.MfClientHttpRequestFactoryImpl">
<constructor-arg index="0" value="${maxConnectionsTotal}" />
<constructor-arg index="1" value="${maxConnectionsPerRoute}"/>
<constructor-arg index="2" ref="jobManager"/>
<constructor-arg index="2" value="${http.connectionRequestTimeout}"/>
<constructor-arg index="3" value="${http.connectTimeout}"/>
<constructor-arg index="4" value="${http.socketTimeout}"/>
</bean>
<bean id="metricNameStrategy" class="org.mapfish.print.metrics.MetricsNameStrategyFactory" factory-method="hostAndMethod" />
<bean id="loggingMetricsConfigurator" class="org.mapfish.print.metrics.LoggingMetricsConfigurator" lazy-init="false"/>
Expand Down
16 changes: 15 additions & 1 deletion core/src/main/resources/mapfish-spring.properties
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,19 @@ httpRequest.fetchRetry.intervalMillis=100
# Amount of time in the past where we check if a print job was executed by this server
healthStatus.expectedMaxTime.sinceLastPrint.InSeconds=300

# Maximum number of Print Jobs queued before raising it i
# Maximum number of Print Jobs queued before raising it is an issue
healthStatus.unhealthyThreshold.maxNbrPrintJobQueued=4

# Number of milliseconds used when requesting a connection from the connection manager.
# Recommended 2s for interactive application
# Recommended 10s for batch application
http.connectionRequestTimeout=10000

# Number of milliseconds until a connection is established.
# Recommended 5s for applications in general
# Recommended 10s for tolerant application with slow connection
http.connectTimeout=10000

# Maximum number of milliseconds during which a socket remains inactive between two consecutive data packets.
# Using 5 minutes by default to support very slow and large downloads
http.socketTimeout=300000
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.mapfish.print.http.ConfigurableRequest;
import org.mapfish.print.http.MfClientHttpRequestFactory;
import org.mapfish.print.http.MfClientHttpRequestFactoryImpl;
import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand All @@ -27,7 +26,7 @@ public class TestHttpClientFactory extends MfClientHttpRequestFactoryImpl
private final Map<Predicate<URI>, Handler> handlers = new ConcurrentHashMap<>();

public TestHttpClientFactory() {
super(20, 10, new ThreadPoolJobManager());
super(20, 10, 1000, 1000, 1000);
}

public void registerHandler(Predicate<URI> matcher, Handler handler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse;

Expand Down Expand Up @@ -41,7 +40,7 @@ public void testGetHeaders() throws Exception {
});

MfClientHttpRequestFactoryImpl factory =
new MfClientHttpRequestFactoryImpl(20, 10, new ThreadPoolJobManager());
new MfClientHttpRequestFactoryImpl(20, 10, 1000, 1000, 1000);
final ConfigurableRequest request =
factory.createRequest(
new URI("http://" + HttpProxyTest.LOCALHOST + ":" + TARGET_PORT + "/request"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="httpClientFactory" class="org.mapfish.print.http.MfClientHttpRequestFactoryImpl">
<constructor-arg index="0" value="20" />
<constructor-arg index="1" value="10" />
<constructor-arg index="2" value="1000" />
<constructor-arg index="3" value="1000" />
<constructor-arg index="4" value="1000" />
</bean>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.mapfish.print.http.MfClientHttpRequestFactoryImpl;
import org.mapfish.print.servlet.job.impl.ThreadPoolJobManager;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
Expand All @@ -22,7 +21,7 @@ public abstract class AbstractApiTest {
protected static final String PRINT_SERVER = "http://print:8080/";

protected ClientHttpRequestFactory httpRequestFactory =
new MfClientHttpRequestFactoryImpl(10, 10, new ThreadPoolJobManager());
new MfClientHttpRequestFactoryImpl(20, 10, 1000, 1000, 1000);

protected ClientHttpRequest getRequest(String path, HttpMethod method)
throws IOException, URISyntaxException {
Expand Down

0 comments on commit 57357fb

Please sign in to comment.