From d61c24acece0864abf5f84ec6c24623cc072e641 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E7=94=9FBug=E9=A5=B2=E5=85=BB=E5=91=98?= Date: Sun, 28 Sep 2025 16:57:07 +0800 Subject: [PATCH] =?UTF-8?q?perf(PluginsContextHolder):=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=8F=92=E4=BB=B6=E5=88=97=E8=A1=A8=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E6=80=A7=E8=83=BD=EF=BC=8C=E4=BD=BF=E7=94=A8=E5=B9=B6=E8=A1=8C?= =?UTF-8?q?=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将单次获取10000条插件列表改为分页并行请求,每次获取20条,共500页 使用线程池和CompletableFuture实现并行请求,提高获取速度 汇总所有分页结果后返回完整列表 --- .../help/context/PluginsContextHolder.java | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/jetbrains/help/context/PluginsContextHolder.java b/src/main/java/com/jetbrains/help/context/PluginsContextHolder.java index ffc358f..ba83059 100644 --- a/src/main/java/com/jetbrains/help/context/PluginsContextHolder.java +++ b/src/main/java/com/jetbrains/help/context/PluginsContextHolder.java @@ -26,7 +26,7 @@ public class PluginsContextHolder { 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/"; @@ -88,19 +88,55 @@ public class PluginsContextHolder { } public static PluginList pluginList() { - return HttpUtil.createGet(PLUGIN_LIST_URL) - .thenFunction(response -> { - try (InputStream is = response.bodyStream()) { - if (!response.isOk()) { - throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求失败! = {}", PLUGIN_LIST_URL, response)); - } - PluginList pluginList = JSONUtil.toBean(IoUtil.readUtf8(is), PluginList.class); - log.info("获取大小 => [{}]", pluginList.getTotal()); - return pluginList; - } catch (IOException e) { - throw new IllegalArgumentException(CharSequenceUtil.format("{} 请求IO读取失败!", PLUGIN_LIST_URL), e); + // 初始化一个空的 PluginList 用于汇总结果 + PluginList resultPluginList = new PluginList(); + resultPluginList.setPlugins(new ArrayList<>()); + resultPluginList.setTotal(0L); + + // 配置线程池,核心线程数 + java.util.concurrent.ExecutorService executor = java.util.concurrent.Executors.newFixedThreadPool(3); + // 创建一个线程池,使用并行流处理请求 + List> futures = new ArrayList<>(); + for (int i = 0; i < 500; i++) { + int offset = i; + String url = PLUGIN_LIST_URL + "&offset=" + offset; + CompletableFuture 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 allFutures = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])); + CompletableFuture> 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 pluginListFilter(PluginList pluginList) {