@@ -11,11 +11,37 @@ use reqwest::{
11
11
} ;
12
12
use tectonic_errors:: { anyhow:: bail, Result } ;
13
13
use tectonic_status_base:: { tt_note, StatusBackend } ;
14
+ use std:: cell:: RefCell ;
14
15
15
16
use crate :: { GetUrlBackend , RangeReader } ;
16
17
17
18
const MAX_HTTP_REDIRECTS_ALLOWED : usize = 10 ;
18
19
20
+ thread_local ! ( static SHARED_CLIENT : RefCell <Option <Client >> = RefCell :: new( None ) ) ;
21
+
22
+ fn get_shared_client ( ) -> Client {
23
+ SHARED_CLIENT . with ( |rclient| {
24
+ let mut oclient = rclient. borrow_mut ( ) ;
25
+ match oclient. to_owned ( ) {
26
+ Some ( client) => client,
27
+ None => {
28
+ let client = Client :: new ( ) ;
29
+ * oclient = Some ( client. clone ( ) ) ;
30
+ client
31
+ }
32
+ }
33
+ } )
34
+ }
35
+
36
+ /// Drop the shared client.
37
+ /// It maintains a connection pool that causes problem when forking.
38
+ pub fn clear_shared_client ( ) {
39
+ SHARED_CLIENT . with ( |rclient| {
40
+ let mut oclient = rclient. borrow_mut ( ) ;
41
+ * oclient = None
42
+ } )
43
+ }
44
+
19
45
/// URL-get backend implemented using the `reqwest` crate.
20
46
#[ derive( Debug , Default ) ]
21
47
pub struct ReqwestBackend { }
@@ -25,7 +51,7 @@ impl GetUrlBackend for ReqwestBackend {
25
51
type RangeReader = ReqwestRangeReader ;
26
52
27
53
fn get_url ( & mut self , url : & str , _status : & mut dyn StatusBackend ) -> Result < Response > {
28
- let res = Client :: new ( ) . get ( url) . send ( ) ?;
54
+ let res = get_shared_client ( ) . get ( url) . send ( ) ?;
29
55
if !res. status ( ) . is_success ( ) {
30
56
bail ! (
31
57
"unexpected HTTP response code {} for URL {}" ,
@@ -114,14 +140,14 @@ impl GetUrlBackend for ReqwestBackend {
114
140
#[ derive( Debug ) ]
115
141
pub struct ReqwestRangeReader {
116
142
url : String ,
117
- client : Client ,
143
+ // client: Rc< Client> ,
118
144
}
119
145
120
146
impl ReqwestRangeReader {
121
147
fn new ( url : & str ) -> ReqwestRangeReader {
122
148
ReqwestRangeReader {
123
149
url : url. to_owned ( ) ,
124
- client : Client :: new ( ) ,
150
+ // client: Rc::new( Client::new() ),
125
151
}
126
152
}
127
153
}
@@ -136,7 +162,8 @@ impl RangeReader for ReqwestRangeReader {
136
162
let mut headers = HeaderMap :: new ( ) ;
137
163
headers. insert ( RANGE , header_val) ;
138
164
139
- let res = self . client . get ( & self . url ) . headers ( headers) . send ( ) ?;
165
+ let client = get_shared_client ( ) ;
166
+ let res = client. get ( & self . url ) . headers ( headers) . send ( ) ?;
140
167
141
168
if res. status ( ) != StatusCode :: PARTIAL_CONTENT {
142
169
bail ! (
0 commit comments