7368322791
安全 - /api/ziniao/** 五个匿名接口加管理员鉴权(此前可匿名换取任意员工店铺登录令牌) - 删除 Flask 遗留后门:默认密码建超管 + 每次启动写生产 users 表(服务端与客户端各一份) - 进度/详情接口归属过滤:新增 TaskProgressOwnershipSupport,11 模块 progress/light 与 /tasks/batch 接入,DTO 补 userId,前端 13 个查询封装补传(未传时后端不过滤,兼容旧端) - 代理提取链接(含账密)不再明文入日志(新增 common/util/SecretMasking) - 全局异常兜底不再回传原始异常信息;内部令牌比较改常量时间 - 登录加失败计数与锁定(10 次锁 15 分钟);品牌源文件下载加 SSRF 防护 - AdminApiGuardFilter 覆盖前缀从 2 扩到 15(开关默认 false,行为不变,为收紧做准备) - 生产关闭 springdoc/knife4j(/doc.html 匿名可读全部接口定义) 正确性 - 40901/40902 拆分:锁竞争不再被伪装成 success=true(此前客户端停止重试、分片静默丢失) - 假成功收敛:集采明细批量写失败改为抛出、去重 worker 异常标失败、4 个 worker 改判 success 字段、publish 空 ASIN 行参与批次 flush、巡店删除全失败带 error 上报 - 客户端心跳 discard 移入 finally(7 模块,失败路径不再留僵尸 RUNNING 任务) - 状态机条件更新:跟价停止循环、集采 activate/fail、imagevideo 归档回填、店铺匹配提交 性能 - 前端入口包 JS 1.05MB→204KB、CSS 355KB→10.7KB(Element Plus 改按需 + el-config-provider) - 载荷引用计数按指针里的 taskId 收敛(原 JSON 列 IN 全表扫且逐行调用) - 店铺明细多值批量 INSERT;快照 upsert 预载缓存;结果文件列改单条 UPDATE - 新增迁移 V120(补 3 个缺失索引)/V121(删 4 个被覆盖的冗余索引)/V122(URL 前缀索引) 稳定性 - 新增 common/util/ThreadPools 有界线程池替换 5 处无界队列(防堆积 OOM) - Redis 锁释放改 Lua 原子校验(原裸 delete 会误删他人已过期的锁) - imagevideo 加死节点接管;锁续期失败重试;调度池 4→16;openStream 全部加超时 - 事务内远程对象删除移到提交后;启动恢复锁按实例命名 客户端 - 不再 taskkill /f /im chrome.exe(改为按调试端口精准回收,不杀用户自己的浏览器) - 密码检测不再无条件杀紫鸟进程;品牌检测加全局互斥(代理池不再互相覆盖) - base_dir 统一到 exe 目录(原被 os.getcwd() 覆盖,日志/缓存会分裂两个目录) - 缓存加定时清理;图片下载加超时;mkstemp 句柄托管 测试 - 同步更新受影响的契约测试(构造器签名/条件更新/方法改名/新增接口方法等) - 修复 FaultInjectionTest 等 3 处 mock 未 stub 流式 read 导致的读循环 OOM - mvn test 2795 个测试全绿
188 lines
8.9 KiB
Java
188 lines
8.9 KiB
Java
package com.nanri.aiimage.config;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
|
|
import com.nanri.aiimage.modules.brand.client.BrandCheckClient.BrandCheckBatchResult;
|
|
import com.nanri.aiimage.modules.similarasin.client.SimilarAsinLlmClient;
|
|
import com.nanri.aiimage.modules.ziniao.client.ZiniaoClientImpl;
|
|
import org.junit.jupiter.api.AfterEach;
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
import org.junit.jupiter.api.Test;
|
|
import org.springframework.http.client.ClientHttpRequestFactory;
|
|
import org.springframework.http.client.JdkClientHttpRequestFactory;
|
|
import org.springframework.web.client.RestClient;
|
|
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.Method;
|
|
import java.net.http.HttpClient;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
import static org.junit.jupiter.api.Assertions.assertNotSame;
|
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
|
import static org.junit.jupiter.api.Assertions.assertSame;
|
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
|
|
|
|
/**
|
|
* Task 77:统一 LLM、品牌检查和紫鸟 HTTP 客户端的连接复用策略。
|
|
* 三个外部客户端统一使用 HttpClientPool 共享的 java.net.http.HttpClient
|
|
* (内置 keep-alive 连接复用):LLM 与品牌检查从无连接池的
|
|
* HttpURLConnection 迁移到共享池;紫鸟从每次调用新建 RestClient 改为
|
|
* 复用共享单例。同一 HttpClient 实例即表明连接复用同一连接池。
|
|
*/
|
|
class HttpClientConnectionReuseTest {
|
|
|
|
private final List<AutoCloseable> closeables = new ArrayList<>();
|
|
|
|
@BeforeEach
|
|
@SuppressWarnings("unchecked")
|
|
void setUp() throws Exception {
|
|
Field sharedField = HttpClientPool.class.getDeclaredField("sharedHttpClient");
|
|
sharedField.setAccessible(true);
|
|
Object previous = sharedField.get(null);
|
|
closeables.add(() -> sharedField.set(null, previous));
|
|
}
|
|
|
|
@AfterEach
|
|
void tearDown() throws Exception {
|
|
for (int i = closeables.size() - 1; i >= 0; i--) {
|
|
closeables.get(i).close();
|
|
}
|
|
}
|
|
|
|
private static ClientHttpRequestFactory factoryOf(RestClient client) throws Exception {
|
|
Field field = client.getClass().getDeclaredField("clientRequestFactory");
|
|
field.setAccessible(true);
|
|
return (ClientHttpRequestFactory) field.get(client);
|
|
}
|
|
|
|
private static HttpClient clientOf(ClientHttpRequestFactory factory) throws Exception {
|
|
Field field = factory.getClass().getDeclaredField("httpClient");
|
|
field.setAccessible(true);
|
|
return (HttpClient) field.get(factory);
|
|
}
|
|
|
|
/**
|
|
* 反射调用私有 restClient(...),模拟真实请求前获取单例。
|
|
* 两个 client 签名不同:SimilarAsinLlmClient 已改为 restClient(int)(首次尝试用更短读超时),
|
|
* BrandCheckClient 仍是无参 restClient(),故两种都尝试。
|
|
*/
|
|
private static RestClient restClientOf(Object client) throws Exception {
|
|
try {
|
|
Method method = client.getClass().getDeclaredMethod("restClient", int.class);
|
|
method.setAccessible(true);
|
|
return (RestClient) method.invoke(client, 1);
|
|
} catch (NoSuchMethodException ignored) {
|
|
Method method = client.getClass().getDeclaredMethod("restClient");
|
|
method.setAccessible(true);
|
|
return (RestClient) method.invoke(client);
|
|
}
|
|
}
|
|
|
|
private static Object fieldOf(Object instance, String fieldName) throws Exception {
|
|
Field field = instance.getClass().getDeclaredField(fieldName);
|
|
field.setAccessible(true);
|
|
return field.get(instance);
|
|
}
|
|
|
|
private static void assertPooled(ClientHttpRequestFactory factory) throws Exception {
|
|
assertTrue(factory instanceof JdkClientHttpRequestFactory,
|
|
"统一使用基于 java.net.http.HttpClient 的连接池工厂,实际 " + factory.getClass().getSimpleName());
|
|
assertSame(HttpClientPool.sharedHttpClient(), clientOf(factory),
|
|
"工厂复用共享 HttpClient 连接池");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_normal_default_path() throws Exception {
|
|
// 默认路径:品牌检查客户端通过共享池创建单例 RestClient,
|
|
// 工厂为带 keep-alive 连接池的 JdkClientHttpRequestFactory。
|
|
BrandCheckClient client = new BrandCheckClient(new BrandCheckProperties(), null);
|
|
BrandCheckBatchResult result = client.checkTitleText(" ");
|
|
|
|
assertTrue(result.brands().isEmpty(), "空标题安全跳过,不创建无效资源");
|
|
assertPooled(factoryOf(restClientOf(client)));
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_normal_multiple_items() throws Exception {
|
|
// 批量场景:Coze/品牌/紫鸟三个客户端各自持有独立 RestClient,
|
|
// 但底层连接池共用同一 HttpClient 实例,不重复创建。
|
|
SimilarAsinLlmClient llm = new SimilarAsinLlmClient(new SimilarAsinProperties(), new ObjectMapper(), null, null);
|
|
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
|
|
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
|
|
|
|
HttpClient llmClient = clientOf(factoryOf(restClientOf(llm)));
|
|
HttpClient brandClient = clientOf(factoryOf(restClientOf(brand)));
|
|
HttpClient ziniaoClient = clientOf(factoryOf(restClientOf(ziniao)));
|
|
|
|
assertSame(llmClient, brandClient, "LLM 与品牌检查共享连接池");
|
|
assertSame(brandClient, ziniaoClient, "品牌检查与紫鸟共享连接池");
|
|
assertSame(HttpClientPool.sharedHttpClient(), llmClient, "与共享单例一致");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_normal_repeated_operation_is_idempotent() throws Exception {
|
|
// 幂等:同一客户端重复触发请求创建逻辑只产生一个 RestClient,
|
|
// 重复调用返回同一实例,不重复创建客户端对象。
|
|
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
|
|
assertSame(restClientOf(brand), restClientOf(brand), "品牌客户端复用同一 RestClient");
|
|
|
|
ZiniaoClientImpl ziniao = new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper());
|
|
assertSame(restClientOf(ziniao), restClientOf(ziniao), "紫鸟客户端复用同一 RestClient");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_boundary_empty_input() throws Exception {
|
|
// 空输入:空品牌列表不发起任何 HTTP 请求、不创建客户端资源。
|
|
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
|
|
BrandCheckBatchResult result = brand.checkAll(List.of(), "Terms");
|
|
|
|
assertTrue(result.brands().isEmpty());
|
|
assertTrue(result.faildData().isEmpty());
|
|
assertNull(fieldOf(brand, "sharedRestClient"), "无请求时不创建 RestClient");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_boundary_single_item() throws Exception {
|
|
// 单元素:单客户端单请求走共享池,工厂带连接池,行为与批量一致。
|
|
BrandCheckClient brand = new BrandCheckClient(new BrandCheckProperties(), null);
|
|
assertPooled(factoryOf(restClientOf(brand)));
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_boundary_limit_and_overflow() throws Exception {
|
|
// 上限/超限:并发创建多个客户端实例共享同一底层 HttpClient,
|
|
// 不随实例数量线性增长连接资源。
|
|
int instances = 8;
|
|
for (int i = 0; i < instances; i++) {
|
|
restClientOf(new BrandCheckClient(new BrandCheckProperties(), null));
|
|
restClientOf(new ZiniaoClientImpl(new ZiniaoProperties(), new ObjectMapper()));
|
|
}
|
|
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
|
|
"8 个客户端共享同一个 HttpClient");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_invalid_input_rejected() throws Exception {
|
|
// 非法参数:非法超时统一钳制到最小正数,不崩溃、行为确定。
|
|
ClientHttpRequestFactory zero = HttpClientPool.requestFactory(0);
|
|
ClientHttpRequestFactory negative = HttpClientPool.requestFactory(-5);
|
|
assertPooled(zero);
|
|
assertPooled(negative);
|
|
assertNotSame(zero, negative, "不同 readTimeout 各自独立工厂实例");
|
|
}
|
|
|
|
@Test
|
|
void test_task_077_brand_dependency_failure_releases_resources() throws Exception {
|
|
// 依赖失败:反复创建/销毁工厂后共享连接池仍稳定复用;
|
|
// 单个工厂创建失败不影响后续复用。
|
|
for (int i = 0; i < 5; i++) {
|
|
assertPooled(HttpClientPool.requestFactory(3000));
|
|
}
|
|
assertSame(HttpClientPool.sharedHttpClient(), HttpClientPool.sharedHttpClient(),
|
|
"多次构造后共享连接池实例不变");
|
|
}
|
|
}
|