mirror of
https://github.com/Athou/commafeed.git
synced 2026-11-02 10:24:48 +00:00
use responseTimeout as the global timeout for the request
This commit is contained in:
@@ -41,7 +41,6 @@ import org.apache.hc.core5.http.nio.AsyncRequestProducer;
|
|||||||
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
import org.apache.hc.core5.http.nio.AsyncResponseConsumer;
|
||||||
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
|
import org.apache.hc.core5.http.nio.entity.AbstractBinAsyncEntityConsumer;
|
||||||
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
||||||
import org.apache.hc.core5.util.Timeout;
|
|
||||||
import org.jboss.resteasy.reactive.common.headers.CacheControlDelegate;
|
import org.jboss.resteasy.reactive.common.headers.CacheControlDelegate;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
@@ -164,7 +163,6 @@ public class HttpGetter {
|
|||||||
HttpClientContext context = HttpClientContext.create();
|
HttpClientContext context = HttpClientContext.create();
|
||||||
context.setRequestConfig(
|
context.setRequestConfig(
|
||||||
RequestConfig.custom()
|
RequestConfig.custom()
|
||||||
.setResponseTimeout(Timeout.of(config.httpClient().responseTimeout()))
|
|
||||||
// causes issues with some feeds
|
// causes issues with some feeds
|
||||||
// see https://github.com/Athou/commafeed/issues/1572
|
// see https://github.com/Athou/commafeed/issues/1572
|
||||||
// and https://issues.apache.org/jira/browse/HTTPCLIENT-2344
|
// and https://issues.apache.org/jira/browse/HTTPCLIENT-2344
|
||||||
@@ -193,13 +191,8 @@ public class HttpGetter {
|
|||||||
client.execute(req, resp, context, callback);
|
client.execute(req, resp, context, callback);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Duration totalTimeout =
|
|
||||||
config.httpClient()
|
|
||||||
.connectTimeout()
|
|
||||||
.plus(config.httpClient().sslHandshakeTimeout())
|
|
||||||
.plus(config.httpClient().responseTimeout());
|
|
||||||
Message<org.apache.hc.core5.http.HttpResponse, byte[]> message =
|
Message<org.apache.hc.core5.http.HttpResponse, byte[]> message =
|
||||||
future.get(totalTimeout.toSeconds(), TimeUnit.SECONDS);
|
future.get(config.httpClient().responseTimeout().toSeconds(), TimeUnit.SECONDS);
|
||||||
|
|
||||||
byte[] content = message.getBody();
|
byte[] content = message.getBody();
|
||||||
int code = message.getHead().getCode();
|
int code = message.getHead().getCode();
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
|
||||||
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
|
||||||
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
import org.apache.hc.client5.http.async.methods.SimpleRequestProducer;
|
||||||
import org.apache.hc.client5.http.config.RequestConfig;
|
|
||||||
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
|
||||||
import org.apache.hc.core5.http.ContentType;
|
import org.apache.hc.core5.http.ContentType;
|
||||||
import org.apache.hc.core5.http.HttpResponse;
|
import org.apache.hc.core5.http.HttpResponse;
|
||||||
@@ -29,7 +28,6 @@ import org.apache.hc.core5.http.message.BasicNameValuePair;
|
|||||||
import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
|
import org.apache.hc.core5.http.nio.entity.DiscardingEntityConsumer;
|
||||||
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
import org.apache.hc.core5.http.nio.support.BasicResponseConsumer;
|
||||||
import org.apache.hc.core5.net.WWWFormCodec;
|
import org.apache.hc.core5.net.WWWFormCodec;
|
||||||
import org.apache.hc.core5.util.Timeout;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -37,6 +35,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
import java.util.concurrent.Future;
|
import java.util.concurrent.Future;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
@Singleton
|
@Singleton
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -105,10 +105,6 @@ public class PushNotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl + "/" + topic);
|
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl + "/" + topic);
|
||||||
request.setRequestConfig(
|
|
||||||
RequestConfig.custom()
|
|
||||||
.setResponseTimeout(Timeout.of(config.httpClient().responseTimeout()))
|
|
||||||
.build());
|
|
||||||
request.addHeader("Title", feedTitle);
|
request.addHeader("Title", feedTitle);
|
||||||
request.setBody(entryTitle, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8));
|
request.setBody(entryTitle, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8));
|
||||||
|
|
||||||
@@ -152,10 +148,6 @@ public class PushNotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl + "/message");
|
SimpleRequestBuilder request = SimpleRequestBuilder.post(serverUrl + "/message");
|
||||||
request.setRequestConfig(
|
|
||||||
RequestConfig.custom()
|
|
||||||
.setResponseTimeout(Timeout.of(config.httpClient().responseTimeout()))
|
|
||||||
.build());
|
|
||||||
request.addHeader("X-Gotify-Key", token);
|
request.addHeader("X-Gotify-Key", token);
|
||||||
request.setBody(json.toString(), ContentType.APPLICATION_JSON);
|
request.setBody(json.toString(), ContentType.APPLICATION_JSON);
|
||||||
|
|
||||||
@@ -187,10 +179,6 @@ public class PushNotificationService {
|
|||||||
|
|
||||||
SimpleRequestBuilder request =
|
SimpleRequestBuilder request =
|
||||||
SimpleRequestBuilder.post("https://api.pushover.net/1/messages.json");
|
SimpleRequestBuilder.post("https://api.pushover.net/1/messages.json");
|
||||||
request.setRequestConfig(
|
|
||||||
RequestConfig.custom()
|
|
||||||
.setResponseTimeout(Timeout.of(config.httpClient().responseTimeout()))
|
|
||||||
.build());
|
|
||||||
request.setBody(
|
request.setBody(
|
||||||
WWWFormCodec.format(params, StandardCharsets.UTF_8),
|
WWWFormCodec.format(params, StandardCharsets.UTF_8),
|
||||||
ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
|
ContentType.APPLICATION_FORM_URLENCODED.withCharset(StandardCharsets.UTF_8));
|
||||||
@@ -205,7 +193,9 @@ public class PushNotificationService {
|
|||||||
new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()),
|
new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()),
|
||||||
null);
|
null);
|
||||||
try {
|
try {
|
||||||
HttpResponse response = future.get().getHead();
|
HttpResponse response =
|
||||||
|
future.get(config.httpClient().responseTimeout().toSeconds(), TimeUnit.SECONDS)
|
||||||
|
.getHead();
|
||||||
if (response.getCode() >= 400) {
|
if (response.getCode() >= 400) {
|
||||||
throw new PushNotificationException(
|
throw new PushNotificationException(
|
||||||
name + " notification failed with status " + response.getCode());
|
name + " notification failed with status " + response.getCode());
|
||||||
@@ -214,7 +204,11 @@ public class PushNotificationService {
|
|||||||
future.cancel(true);
|
future.cancel(true);
|
||||||
Thread.currentThread().interrupt();
|
Thread.currentThread().interrupt();
|
||||||
throw new IOException("interrupted while sending " + name + " notification", e);
|
throw new IOException("interrupted while sending " + name + " notification", e);
|
||||||
|
} catch (TimeoutException e) {
|
||||||
|
future.cancel(true);
|
||||||
|
throw new IOException("timed out while sending " + name + " notification", e);
|
||||||
} catch (ExecutionException e) {
|
} catch (ExecutionException e) {
|
||||||
|
future.cancel(true);
|
||||||
throw new IOException(e.getCause());
|
throw new IOException(e.getCause());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.net.NoRouteToHostException;
|
import java.net.NoRouteToHostException;
|
||||||
import java.net.SocketTimeoutException;
|
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
@@ -241,7 +240,7 @@ class HttpGetterTest {
|
|||||||
.when(HttpRequest.request().withMethod("GET"))
|
.when(HttpRequest.request().withMethod("GET"))
|
||||||
.respond(HttpResponse.response().withDelay(Delay.milliseconds(1000)));
|
.respond(HttpResponse.response().withDelay(Delay.milliseconds(1000)));
|
||||||
|
|
||||||
Assertions.assertThrows(SocketTimeoutException.class, () -> getter.get(this.feedUrl));
|
Assertions.assertThrows(IOException.class, () -> getter.get(this.feedUrl));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user