fix(货源): 主图重试换备用域名绕开被钉死的坏 CDN 节点
根因不是"瞬时抖动":m.media-amazon.com 的多 CDN 池里 Akamai 系边缘没配这个 虚拟主机名,带该 SNI 一律 RST(同 IP 不带 SNI 或用 images-na.ssl-images-amazon.com 则正常),而 JVM 正向 DNS 缓存会把坏节点钉住一整批 → 秒级重试三次全打在它身上。 实测最近 20 条失败 URL 走备用域名 20/20 成功且 md5 与原域名逐字节相同。
This commit is contained in:
+65
@@ -174,6 +174,71 @@ class SimilarAsinMainImageDownloadRetryTest {
|
||||
verify(client, never()).send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 按调用顺序记录请求 URI;前 {@code failTimes} 次抛连接级异常,之后返回 200。
|
||||
*/
|
||||
private HttpClient recordingClient(int failTimes, List<String> uris) {
|
||||
HttpClient client = mock(HttpClient.class);
|
||||
try {
|
||||
when(client.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
uris.add(invocation.getArgument(0, HttpRequest.class).uri().toString());
|
||||
if (uris.size() <= failTimes) {
|
||||
throw new IOException(RESET);
|
||||
}
|
||||
return new StubResponse(200, BODY);
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
@Test
|
||||
void 连接级失败后改用备用域名重试() throws Throwable {
|
||||
List<String> uris = new java.util.ArrayList<>();
|
||||
SimilarAsinLlmService svc = service(new SimilarAsinProperties(), recordingClient(1, uris));
|
||||
|
||||
assertArrayEquals(BODY, (byte[]) invokeDownload(svc, URL));
|
||||
|
||||
assertEquals(2, uris.size());
|
||||
assertTrue(uris.get(0).contains("m.media-amazon.com"), "首试必须用原域名,不改变正常路径行为: " + uris.get(0));
|
||||
assertTrue(uris.get(1).contains("images-na.ssl-images-amazon.com"), "第二次必须换到备用域名: " + uris.get(1));
|
||||
assertTrue(uris.get(1).endsWith("/images/I/61Yh2j3VZmL._AC_SX679_.jpg"), "只换 host,路径与尺寸后缀必须原样: " + uris.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 备用域名映射置空时不换域名() throws Throwable {
|
||||
SimilarAsinProperties props = new SimilarAsinProperties();
|
||||
props.setLlmImageHostAliasMap("");
|
||||
List<String> uris = new java.util.ArrayList<>();
|
||||
|
||||
assertArrayEquals(BODY, (byte[]) invokeDownload(service(props, recordingClient(2, uris)), URL));
|
||||
|
||||
assertEquals(3, uris.size());
|
||||
assertTrue(uris.stream().allMatch(u -> u.equals(URL)), "置空即完全退回旧行为: " + uris);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 映射外的域名不受影响() throws Throwable {
|
||||
List<String> uris = new java.util.ArrayList<>();
|
||||
String alicdn = "https://cbu01.alicdn.com/img/ibank/x.jpg";
|
||||
|
||||
assertArrayEquals(BODY, (byte[]) invokeDownload(service(new SimilarAsinProperties(), recordingClient(1, uris)), alicdn));
|
||||
|
||||
assertEquals(List.of(alicdn, alicdn), uris, "阿里图床没有备用域名,不该被动到");
|
||||
}
|
||||
|
||||
@Test
|
||||
void 带端口与query的URL换域名后其余部分不变() throws Throwable {
|
||||
List<String> uris = new java.util.ArrayList<>();
|
||||
String url = "https://m.media-amazon.com:443/images/I/x.jpg?foo=bar%20baz";
|
||||
|
||||
assertArrayEquals(BODY, (byte[]) invokeDownload(service(new SimilarAsinProperties(), recordingClient(1, uris)), url));
|
||||
|
||||
assertEquals("https://images-na.ssl-images-amazon.com:443/images/I/x.jpg?foo=bar%20baz", uris.get(1));
|
||||
}
|
||||
|
||||
private static final class StubResponse implements HttpResponse<byte[]> {
|
||||
private final int statusCode;
|
||||
private final byte[] body;
|
||||
|
||||
Reference in New Issue
Block a user