Skip to content

Commit

Permalink
Expand the set of retryable exceptions in JdkHttpSender (#5942)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg committed Nov 10, 2023
1 parent 04f6d9c commit f44d991
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.Map;
Expand All @@ -32,6 +31,7 @@
import java.util.zip.GZIPOutputStream;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLException;

/**
* {@link HttpSender} which is backed by JDK {@link HttpClient}.
Expand Down Expand Up @@ -221,7 +221,13 @@ private HttpResponse<byte[]> sendRequest(
}

private static boolean isRetryableException(IOException throwable) {
return throwable instanceof HttpTimeoutException;
// Almost all IOExceptions we've encountered are transient retryable, so we opt out of specific
// IOExceptions that are unlikely to resolve rather than opting in.
// Known retryable IOException messages: "Connection reset", "/{remote ip}:{remote port} GOAWAY
// received"
// Known retryable HttpTimeoutException messages: "request timed out"
// Known retryable HttpConnectTimeoutException messages: "HTTP connect timed out"
return !(throwable instanceof SSLException);
}

private static class NoCopyByteArrayOutputStream extends ByteArrayOutputStream {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.net.http.HttpConnectTimeoutException;
import java.time.Duration;
import java.util.Collections;
import javax.net.ssl.SSLException;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -69,9 +70,20 @@ void sendInternal_RetryableConnectTimeoutException() throws IOException, Interru
verify(mockHttpClient, times(2)).send(any(), any());
}

@Test
void sendInternal_RetryableIoException() throws IOException, InterruptedException {
doThrow(new IOException("error!")).when(mockHttpClient).send(any(), any());

assertThatThrownBy(() -> sender.sendInternal(marshaler -> {}))
.isInstanceOf(IOException.class)
.hasMessage("error!");

verify(mockHttpClient, times(2)).send(any(), any());
}

@Test
void sendInternal_NonRetryableException() throws IOException, InterruptedException {
doThrow(new IOException("unknown error")).when(mockHttpClient).send(any(), any());
doThrow(new SSLException("unknown error")).when(mockHttpClient).send(any(), any());

assertThatThrownBy(() -> sender.sendInternal(marshaler -> {}))
.isInstanceOf(IOException.class)
Expand Down

0 comments on commit f44d991

Please sign in to comment.