From 78757950aa422c438b2f23623432cdb15469d0b0 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 23 Apr 2025 19:28:30 +0800 Subject: [PATCH] fix(route/news): use p-map to limit concurrency (#18927) --- lib/routes/anthropic/news.ts | 10 ++++++---- package.json | 1 + pnpm-lock.yaml | 9 +++++++++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/routes/anthropic/news.ts b/lib/routes/anthropic/news.ts index 3f6371cd9..05eb41f01 100644 --- a/lib/routes/anthropic/news.ts +++ b/lib/routes/anthropic/news.ts @@ -2,6 +2,7 @@ import ofetch from '@/utils/ofetch'; import { load } from 'cheerio'; import cache from '@/utils/cache'; import { Route } from '@/types'; +import pMap from 'p-map'; export const route: Route = { path: '/news', @@ -39,8 +40,9 @@ async function handler() { }; }); - const out = await Promise.all( - list.map((item) => + const out = await pMap( + list, + (item) => cache.tryGet(item.link, async () => { const response = await ofetch(item.link); const $ = load(response); @@ -62,8 +64,8 @@ async function handler() { item.description = content.html(); return item; - }) - ) + }), + { concurrency: 5 } ); return { diff --git a/package.json b/package.json index a8998bf6e..af2c24ebb 100644 --- a/package.json +++ b/package.json @@ -107,6 +107,7 @@ "oauth-1.0a": "2.2.6", "ofetch": "1.4.1", "otplib": "12.0.1", + "p-map": "7.0.3", "pac-proxy-agent": "7.2.0", "proxy-chain": "2.5.8", "puppeteer": "22.6.2", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 295bf93bb..459eee3d7 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -188,6 +188,9 @@ importers: otplib: specifier: 12.0.1 version: 12.0.1 + p-map: + specifier: 7.0.3 + version: 7.0.3 pac-proxy-agent: specifier: 7.2.0 version: 7.2.0 @@ -5053,6 +5056,10 @@ packages: resolution: {integrity: sha512-LaNjtRWUBY++zB5nE/NwcaoMylSPk+S+ZHNB1TzdbMJMny6dynpAGt7X/tl/QYq3TIeE6nxHppbo2LGymrG5Pw==} engines: {node: '>=10'} + p-map@7.0.3: + resolution: {integrity: sha512-VkndIv2fIB99swvQoA65bm+fsmt6UNdGeIB0oxBs+WhAhdh08QA04JXpI7rbB9r08/nkbysKoya9rtDERYOYMA==} + engines: {node: '>=18'} + pac-proxy-agent@7.2.0: resolution: {integrity: sha512-TEB8ESquiLMc0lV8vcd5Ql/JAKAoyzHFXaStwjkzpOpC5Yv+pIzLfHvjTSdf3vpa2bMiUQrg9i6276yn8666aA==} engines: {node: '>= 14'} @@ -11546,6 +11553,8 @@ snapshots: dependencies: p-limit: 3.1.0 + p-map@7.0.3: {} + pac-proxy-agent@7.2.0: dependencies: '@tootallnate/quickjs-emscripten': 0.23.0