mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 08:55:17 -05:00
fix(codeartifact): retry npm GET transport failures once (#9010)
* fix(codeartifact): retry npm GET transport failures once Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
9f0390b340
commit
76f9481bb1
2 files changed
+123
-1
No files matched your search
@@ -13,6 +13,7 @@ import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodyHandlers;
|
||||
import java.net.http.HttpTimeoutException;
|
||||
import java.nio.file.Path;
|
||||
import java.time.Duration;
|
||||
import java.util.Map;
|
||||
@@ -184,7 +185,7 @@ public class NpmExternalConnectionClient {
|
||||
.header("Accept", accept)
|
||||
.GET()
|
||||
.build();
|
||||
response = httpClient.send(request, BodyHandlers.ofInputStream());
|
||||
response = sendWithTransportRetry(request);
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
@@ -207,6 +208,20 @@ public class NpmExternalConnectionClient {
|
||||
}
|
||||
}
|
||||
|
||||
private HttpResponse<InputStream> sendWithTransportRetry(HttpRequest request)
|
||||
throws IOException, InterruptedException {
|
||||
try {
|
||||
return httpClient.send(request, BodyHandlers.ofInputStream());
|
||||
} catch (HttpTimeoutException e) {
|
||||
throw e;
|
||||
} catch (IOException e) {
|
||||
// Retry the idempotent GET once before a response body has been handed to its consumer.
|
||||
// Body failures are handled separately, so partial files and buffers are never replayed.
|
||||
LOG.debugv("Retrying public npm request after transport failure: {0}", e.getMessage());
|
||||
return httpClient.send(request, BodyHandlers.ofInputStream());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The real registry's single-package resource path: a bare, percent-encoded package name, or
|
||||
|
||||
+107
@@ -6,9 +6,14 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpTimeoutException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
@@ -21,6 +26,11 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The wire contract towards the real public npm registry, exercised against a local {@code
|
||||
@@ -63,6 +73,103 @@ class NpmExternalConnectionClientTest {
|
||||
assertArrayEquals(content, result.get().content());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentRetriesAConnectionResetBeforeReceivingTheBody() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
HttpResponse<InputStream> response = mock(HttpResponse.class);
|
||||
byte[] content = "{\"name\":\"widget\"}".getBytes(StandardCharsets.UTF_8);
|
||||
when(response.statusCode()).thenReturn(200);
|
||||
when(response.body()).thenReturn(new ByteArrayInputStream(content));
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
||||
.thenThrow(new IOException("Connection reset"))
|
||||
.thenReturn(response);
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
Optional<NpmExternalConnectionClient.FetchedAsset> result =
|
||||
retryClient.fetchPackument("fake", null, "widget");
|
||||
|
||||
assertTrue(result.isPresent());
|
||||
assertArrayEquals(content, result.get().content());
|
||||
verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentDoesNotRetryARequestTimeout() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
||||
.thenThrow(new HttpTimeoutException("request timed out"));
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
|
||||
|
||||
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentStopsAfterTwoFailedTransportAttempts() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
||||
.thenThrow(new IOException("Connection reset"));
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
|
||||
|
||||
verify(transport, times(2)).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentDoesNotRetryAnHttpNotFoundResponse() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
HttpResponse<InputStream> response = mock(HttpResponse.class);
|
||||
when(response.statusCode()).thenReturn(404);
|
||||
when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0]));
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response);
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
|
||||
|
||||
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentDoesNotReplayAPartiallyConsumedBody() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
HttpResponse<InputStream> response = mock(HttpResponse.class);
|
||||
InputStream body = mock(InputStream.class);
|
||||
when(response.statusCode()).thenReturn(200);
|
||||
when(response.body()).thenReturn(body);
|
||||
when(body.transferTo(any())).thenThrow(new IOException("Connection reset while reading"));
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(response);
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
|
||||
|
||||
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
verify(body).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchPackumentPreservesInterruptionWithoutRetrying() throws Exception {
|
||||
HttpClient transport = mock(HttpClient.class);
|
||||
when(transport.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
||||
.thenThrow(new InterruptedException("cancelled"));
|
||||
NpmExternalConnectionClient retryClient =
|
||||
new NpmExternalConnectionClient(transport, Map.of("fake", "http://127.0.0.1"));
|
||||
|
||||
try {
|
||||
assertTrue(retryClient.fetchPackument("fake", null, "widget").isEmpty());
|
||||
assertTrue(Thread.currentThread().isInterrupted());
|
||||
verify(transport).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
} finally {
|
||||
Thread.interrupted();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Confirmed live against the real registry: a scoped package's single-package resource needs
|
||||
* its scope separator percent-encoded as {@code %2F} to reach the package itself rather than a
|
||||
|
||||
Reference in new issue
Block a user