perf(PluginsContextHolder): 优化插件列表获取性能,使用并行请求

将单次获取10000条插件列表改为分页并行请求,每次获取20条,共500页
使用线程池和CompletableFuture实现并行请求,提高获取速度
汇总所有分页结果后返回完整列表
This commit is contained in:
野生Bug饲养员 2025-09-28 16:57:07 +08:00
parent 30b860c437
commit d61c24acec

View File

@ -26,7 +26,7 @@ public class PluginsContextHolder {
private static final String PLUGIN_BASIC_URL = "https://plugins.jetbrains.com"; private static final String PLUGIN_BASIC_URL = "https://plugins.jetbrains.com";
private static final String PLUGIN_LIST_URL = PLUGIN_BASIC_URL + "/api/searchPlugins?max=10000&offset=0&orderBy=name"; private static final String PLUGIN_LIST_URL = PLUGIN_BASIC_URL + "/api/searchPlugins?max=20&orderBy=name";
private static final String PLUGIN_INFO_URL = PLUGIN_BASIC_URL + "/api/plugins/"; private static final String PLUGIN_INFO_URL = PLUGIN_BASIC_URL + "/api/plugins/";
@ -88,19 +88,55 @@ public class PluginsContextHolder {
} }
public static PluginList pluginList() { public static PluginList pluginList() {
return HttpUtil.createGet(PLUGIN_LIST_URL) // 初始化一个空的 PluginList 用于汇总结果
.thenFunction(response -> { PluginList resultPluginList = new PluginList();
try (InputStream is = response.bodyStream()) { resultPluginList.setPlugins(new ArrayList<>());
if (!response.isOk()) { resultPluginList.setTotal(0L);
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", PLUGIN_LIST_URL, response));
} // 配置线程池核心线程数
PluginList pluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class); java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(3);
log.info("获取大小 => [{}]", pluginList.getTotal()); // 创建一个线程池使用并行流处理请求
return pluginList; List<CompletableFuture<PluginList>> futures = new ArrayList<>();
} catch (IOException e) { for (int i = 0; i < 500; i++) {
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", PLUGIN_LIST_URL), e); int offset = i;
String url = PLUGIN_LIST_URL + "&offset=" + offset;
CompletableFuture<PluginList> future = CompletableFuture.supplyAsync(() -> {
try (InputStream is = HttpUtil.createGet(url).execute().bodyStream()) {
cn.hutool.http.HttpResponse response = HttpUtil.createGet(url).execute();
if (!response.isOk()) {
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", url, response));
} }
}); PluginList currentPluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class);
log.info("获取大小 => [{}], 当前第 => [{}] 页",
currentPluginList.getPlugins() != null ? currentPluginList.getPlugins().size() : 0, offset);
return currentPluginList;
} catch (IOException e) {
throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", url), e);
}
}, executor);
futures.add(future);
}
// 等待所有请求完成
CompletableFuture<Void> allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
CompletableFuture<List<PluginList>> allResults = allFutures.thenApply(v ->
futures.stream()
.map(CompletableFuture::join)
.toList()
);
// 汇总结果
allResults.join().forEach(currentPluginList -> {
if (currentPluginList.getPlugins() != null) {
resultPluginList.getPlugins().addAll(currentPluginList.getPlugins());
resultPluginList.setTotal(resultPluginList.getTotal() + currentPluginList.getPlugins().size());
}
});
// 关闭线程池
executor.shutdown();
return resultPluginList;
} }
public static List<PluginList.Plugin> pluginListFilter(PluginList pluginList) { public static List<PluginList.Plugin> pluginListFilter(PluginList pluginList) {