feat: add max concurrent

This commit is contained in:
zhu327 2024-08-23 11:40:31 +08:00
parent 0ee735299e
commit 30c7571208

View File

@ -1,5 +1,5 @@
use base64; use base64;
use futures::future::join_all; use futures::{stream, StreamExt};
use reqwest::header::{AUTHORIZATION, CONTENT_TYPE}; use reqwest::header::{AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashSet; use std::collections::HashSet;
@ -237,12 +237,15 @@ async fn scheduled(_event: ScheduledEvent, env: Env, _ctx: ScheduleContext) {
.unwrap(); .unwrap();
// 生成摘要并更新的并发任务 // 生成摘要并更新的并发任务
let tasks: Vec<_> = entries let max_concurrent_tasks = 5;
.entries
.into_iter()
.map(|entry| generate_and_update_entry(&config, entry))
.collect();
// 执行所有任务并等待结果 // Create a stream to process tasks with concurrency limit
join_all(tasks).await; let _: Vec<_> = stream::iter(entries.entries)
.map(|entry| {
let config = &config;
async move { generate_and_update_entry(config, entry).await }
})
.buffer_unordered(max_concurrent_tasks)
.collect()
.await;
} }