Skip to content

Commit

Permalink
fix: channel wait
Browse files Browse the repository at this point in the history
  • Loading branch information
baerwang committed Jan 9, 2024
1 parent 2ea8840 commit ab23226
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
42 changes: 0 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,3 @@ regex = "1.10.2"
url = "2.5.0"
tokio = { version = "1.35.1", features = ["full"] }
dashmap = { version = "5.4", features = ["serde"] }
futures = "0.3.30"
20 changes: 13 additions & 7 deletions src/handler/sitemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::HashSet;
use std::sync::{Arc, Mutex};
use std::time;

use futures::stream::StreamExt;
use reqwest::header::{HeaderMap, USER_AGENT};
use serde_derive::Deserialize;

Expand Down Expand Up @@ -56,18 +55,25 @@ pub async fn sitemap(site: String) -> Result<HashSet<String>, Box<dyn std::error

let client = reqwest::Client::new();

futures::stream::iter(values.into_iter().map(|v| {
let mut handles = Vec::new();

for v in values {
let ua_clone = ua.clone();
let client_clone = client.clone();
let loc_set_clone = Arc::clone(&loc_set);
tokio::task::spawn(async move {
let handle = tokio::task::spawn(async move {
let result = parse_sitemap(v.to_string(), ua_clone, client_clone).await;
let mut inner_set = loc_set_clone.lock().unwrap();
inner_set.extend(result.unwrap_or_default());
})
}))
.for_each(|_| async {})
.await;
});
handles.push(handle);
}

while let Some(handle) = handles.pop() {
if let Err(err) = handle.await {
log::error!("handles failed:{:?}", err)
}
}

Ok(Arc::try_unwrap(loc_set).unwrap().into_inner().unwrap())
}
Expand Down

0 comments on commit ab23226

Please sign in to comment.