File tree Expand file tree Collapse file tree 2 files changed +25
-17
lines changed Expand file tree Collapse file tree 2 files changed +25
-17
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ use std::{collections::BTreeMap, sync::OnceLock};
33use rust_team_data:: v1;
44use serde:: de:: DeserializeOwned ;
55
6+ use crate :: util:: in_thread;
7+
68trait 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}
Original file line number Diff line number Diff 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
128130pub 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+ }
You can’t perform that action at this time.
0 commit comments