Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(sdk-java): bound the whole exchange by the request timeout (#1803)
Yudhi Armyndharise9142adadbd7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex e1e9b0922..46218e67c 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Apply the Java SDK request timeout to the response body as well as the headers.- Keep a new dashboard template draft when an earlier template save finishes.- Stop the dashboard Chats page from marking the open chat read on a newly selected session.- Refuse bulk batches once shutdown begins, and fail a batch saved during shutdown instead of sending it.diff --git a/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java b/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.javaindex 8d42adb59..0d109dd89 100644--- a/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java+++ b/sdk/java/src/main/java/com/rmyndharis/openwa/http/DefaultHttpTransport.java@@ -7,6 +7,10 @@ import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;import java.net.http.HttpTimeoutException;+import java.util.concurrent.CompletableFuture;+import java.util.concurrent.ExecutionException;+import java.util.concurrent.TimeUnit;+import java.util.concurrent.TimeoutException;/** Default transport backed by {@link java.net.http.HttpClient}. Never follows redirects. */public final class DefaultHttpTransport implements HttpTransport {@@ -23,11 +27,31 @@ public final class DefaultHttpTransport implements HttpTransport {.timeout(req.timeout()).method(req.method().name(), pub);req.headers().forEach(b::header);+ // The request timeout only covers the wait for response headers; bounding the future+ // also covers a server that stalls while sending the body.+ CompletableFuture<HttpResponse<byte[]>> future =+ client.sendAsync(b.build(), HttpResponse.BodyHandlers.ofByteArray());try {- HttpResponse<byte[]> res = client.send(b.build(), HttpResponse.BodyHandlers.ofByteArray());+ HttpResponse<byte[]> res = future.get(req.timeout().toMillis(), TimeUnit.MILLISECONDS);return new HttpResponseData(res.statusCode(), res.headers().map(), res.body());- } catch (HttpTimeoutException e) {+ } catch (TimeoutException e) {+ future.cancel(true);throw new OpenWATimeoutError(req.timeout().toMillis());+ } catch (InterruptedException e) {+ future.cancel(true);+ throw e;+ } catch (ExecutionException e) {+ Throwable cause = e.getCause();+ if (cause instanceof HttpTimeoutException) {+ throw new OpenWATimeoutError(req.timeout().toMillis());+ }+ if (cause instanceof IOException io) {+ throw io;+ }+ if (cause instanceof RuntimeException re) {+ throw re;+ }+ throw new IOException(cause.getMessage(), cause);}}}diff --git a/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java b/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.javaindex 8fd9c257d..0a8290ed5 100644--- a/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java+++ b/sdk/java/src/test/java/com/rmyndharis/openwa/http/DefaultHttpTransportTest.java@@ -10,6 +10,8 @@ import com.rmyndharis.openwa.errors.OpenWATimeoutError;import com.sun.net.httpserver.HttpServer;import java.net.InetSocketAddress;import java.time.Duration;+import java.util.concurrent.CountDownLatch;+import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicReference;import org.junit.jupiter.api.AfterEach;@@ -20,6 +22,7 @@ import org.junit.jupiter.api.Test;class DefaultHttpTransportTest {private HttpServer server;private String baseUrl;+ private final CountDownLatch release = new CountDownLatch(1);@BeforeEachvoid start() throws Exception {@@ -30,6 +33,7 @@ class DefaultHttpTransportTest {@AfterEachvoid stop() {+ release.countDown();server.stop(0);}@@ -77,4 +81,23 @@ class DefaultHttpTransportTest {assertThrows(OpenWATimeoutError.class,() -> client(Duration.ofMillis(100)).requestVoid(HttpMethod.GET, "/slow", null, null));}++ @Test+ void mapsABodyStallToOpenWATimeoutError() {+ server.createContext("/stall", ex -> {+ try {+ ex.sendResponseHeaders(200, 2);+ ex.getResponseBody().write('{');+ ex.getResponseBody().flush();+ release.await(10, TimeUnit.SECONDS);+ } catch (InterruptedException e) {+ Thread.currentThread().interrupt();+ } finally {+ ex.close();+ }+ });++ assertThrows(OpenWATimeoutError.class,+ () -> client(Duration.ofMillis(100)).requestVoid(HttpMethod.GET, "/stall", null, null));+ }}