Skip to content

Commit 1b1c674

Browse files
authored
Merge pull request #146 from nikomatsakis/launch-2025h1
use std::thread::scope
2 parents 26e2bbc + f860123 commit 1b1c674

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

mdbook-goals/src/team.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::{collections::BTreeMap, sync::OnceLock};
33
use rust_team_data::v1;
44
use serde::de::DeserializeOwned;
55

6+
use crate::util::in_thread;
7+
68
trait Load<T> {
79
fn load(&self, op: impl FnOnce() -> anyhow::Result<T>) -> anyhow::Result<&T>;
810
}
@@ -112,13 +114,8 @@ where
112114
// Run this on another thread because it can create a tokio runtime
113115
// for the block reqwest API which makes tokio grouchy when that runtime is
114116
// dropped.
115-
std::thread::scope(|scope| {
116-
scope
117-
.spawn(|| {
118-
let url = format!("{}/{}", v1::BASE_URL, path);
119-
Ok(reqwest::blocking::get(&url)?.json()?)
120-
})
121-
.join()
122-
.unwrap()
117+
in_thread(|| {
118+
let url = format!("{}/{}", v1::BASE_URL, path);
119+
Ok(reqwest::blocking::get(&url)?.json()?)
123120
})
124121
}

mdbook-goals/src/util.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,17 @@ impl GithubUserInfo {
7676
}
7777

7878
fn github_request(login: &str) -> anyhow::Result<Self> {
79-
// FIXME: cache this in the target directory or something
80-
use reqwest::header::USER_AGENT;
81-
let url = format!("https://api.github.com/users/{}", &login[1..]);
82-
let response: GithubUserInfo = reqwest::blocking::Client::new()
83-
.get(&url)
84-
.header(USER_AGENT, "mdbook-goals/1.0")
85-
.send()?
86-
.json()?;
87-
Ok(response)
79+
in_thread(|| {
80+
// FIXME: cache this in the target directory or something
81+
use reqwest::header::USER_AGENT;
82+
let url = format!("https://api.github.com/users/{}", &login[1..]);
83+
let response: GithubUserInfo = reqwest::blocking::Client::new()
84+
.get(&url)
85+
.header(USER_AGENT, "mdbook-goals/1.0")
86+
.send()?
87+
.json()?;
88+
Ok(response)
89+
})
8890
}
8991
}
9092

@@ -128,3 +130,12 @@ pub fn markdown_files(directory_path: &Path) -> anyhow::Result<Vec<(PathBuf, Pat
128130
pub fn comma(s: &BTreeSet<String>) -> String {
129131
s.iter().map(|s| &s[..]).collect::<Vec<_>>().join(",")
130132
}
133+
134+
/// Runs `op` in another thread. Useful for making blocking calls to `request`
135+
/// without making tokio upset.
136+
pub fn in_thread<R>(op: impl FnOnce() -> R + Send) -> R
137+
where
138+
R: Send,
139+
{
140+
std::thread::scope(|scope| scope.spawn(|| op()).join().unwrap())
141+
}

0 commit comments

Comments
 (0)