Skip to content

Commit

Permalink
fix(route/bjnews): Add concurrent request throttling (#16947)
Browse files Browse the repository at this point in the history
  • Loading branch information
dzx-dzx authored Sep 27, 2024
1 parent 11d67a2 commit 0211583
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/routes/bjnews/cat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { load } from 'cheerio';
import ofetch from '@/utils/ofetch';

import { fetchArticle } from './utils';
import asyncPool from 'tiny-async-pool';

export const route: Route = {
path: '/cat/:cat',
Expand Down Expand Up @@ -34,10 +35,17 @@ async function handler(ctx) {
category: $(a).parent().find('.source').text().trim(),
}));

const out = await Promise.all(list.map((item) => fetchArticle(item)));
const out = await asyncPoolAll(ctx.req.query('pool') ? Number.parseInt(ctx.req.query('pool')) : 1, list, (item) => fetchArticle(item));
return {
title: `新京报 - 分类 - ${$('.cur').text().trim()}`,
link: url,
item: out,
};
}
async function asyncPoolAll<IN, OUT>(poolLimit: number, array: readonly IN[], iteratorFn: (generator: IN) => Promise<OUT>) {
const results: Awaited<OUT[]> = [];
for await (const result of asyncPool(poolLimit, array, iteratorFn)) {
results.push(result);
}
return results;
}

0 comments on commit 0211583

Please sign in to comment.