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};
3
3
use rust_team_data:: v1;
4
4
use serde:: de:: DeserializeOwned ;
5
5
6
+ use crate :: util:: in_thread;
7
+
6
8
trait Load < T > {
7
9
fn load ( & self , op : impl FnOnce ( ) -> anyhow:: Result < T > ) -> anyhow:: Result < & T > ;
8
10
}
@@ -112,13 +114,8 @@ where
112
114
// Run this on another thread because it can create a tokio runtime
113
115
// for the block reqwest API which makes tokio grouchy when that runtime is
114
116
// 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 ( ) ?)
123
120
} )
124
121
}
Original file line number Diff line number Diff line change @@ -76,15 +76,17 @@ impl GithubUserInfo {
76
76
}
77
77
78
78
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
+ } )
88
90
}
89
91
}
90
92
@@ -128,3 +130,12 @@ pub fn markdown_files(directory_path: &Path) -> anyhow::Result<Vec<(PathBuf, Pat
128
130
pub fn comma ( s : & BTreeSet < String > ) -> String {
129
131
s. iter ( ) . map ( |s| & s[ ..] ) . collect :: < Vec < _ > > ( ) . join ( "," )
130
132
}
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