Skip to content

Commit c293c27

Browse files
authored
fix: Move reqwest init off the main thread (#186)
fixes #184
1 parent 03175a1 commit c293c27

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ rustc_version = { version = "0.2.3", optional = true }
6767
failure_derive = "0.1.6"
6868
pretty_env_logger = "0.4.0"
6969
actix-web = { version = "0.7.19", default-features = false }
70+
tokio = { version = "0.2", features = ["full"] }
7071

7172
[[example]]
7273
name = "error-chain-demo"

src/transport.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,30 @@ implement_http_transport! {
234234
signal: Arc<Condvar>,
235235
shutdown_immediately: Arc<AtomicBool>,
236236
queue_size: Arc<Mutex<usize>>,
237-
http_client: Client,
237+
http_client: Option<Client>,
238238
) {
239239
let dsn = options.dsn.clone().unwrap();
240240
let user_agent = options.user_agent.to_string();
241241

242242
let mut disabled = None::<SystemTime>;
243+
let http_proxy = options.http_proxy.as_ref().map(ToString::to_string);
244+
let https_proxy = options.https_proxy.as_ref().map(ToString::to_string);
243245

244246
thread::Builder::new()
245247
.name("sentry-transport".to_string())
246248
.spawn(move || {
247249
sentry_debug!("spawning reqwest transport");
250+
let http_client = http_client.unwrap_or_else(|| {
251+
let mut builder = Client::builder();
252+
if let Some(url) = http_proxy {
253+
builder = builder.proxy(Proxy::http(&url).unwrap());
254+
};
255+
if let Some(url) = https_proxy {
256+
builder = builder.proxy(Proxy::https(&url).unwrap());
257+
};
258+
builder.build().unwrap()
259+
});
260+
248261
let url = dsn.store_api_url().to_string();
249262

250263
while let Some(event) = receiver.recv().unwrap_or(None) {
@@ -301,19 +314,8 @@ implement_http_transport! {
301314
}).unwrap()
302315
}
303316

304-
fn http_client(options: &ClientOptions, client: Option<Client>) -> Client {
305-
client.unwrap_or_else(|| {
306-
let http_proxy = options.http_proxy.as_ref().map(ToString::to_string);
307-
let https_proxy = options.https_proxy.as_ref().map(ToString::to_string);
308-
let mut client = Client::builder();
309-
if let Some(url) = http_proxy {
310-
client = client.proxy(Proxy::http(&url).unwrap());
311-
};
312-
if let Some(url) = https_proxy {
313-
client = client.proxy(Proxy::https(&url).unwrap());
314-
};
315-
client.build().unwrap()
316-
})
317+
fn http_client(_options: &ClientOptions, client: Option<Client>) -> Option<Client> {
318+
client
317319
}
318320
}
319321

@@ -421,8 +423,7 @@ implement_http_transport! {
421423
match handle.response_code() {
422424
Ok(429) => {
423425
if let Some(retry_after) = retry_after
424-
.as_ref()
425-
.map(String::as_str)
426+
.as_deref()
426427
.and_then(parse_retry_after)
427428
{
428429
disabled = Some(retry_after);

tests/test_async.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// See https://github.com/getsentry/sentry-rust/issues/184
2+
#[tokio::test]
3+
async fn test_nested_async_runtimes() {
4+
let _guard = sentry::init("https://[email protected]/42");
5+
}

tests/test_basic.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,7 @@ fn test_breadcrumbs() {
6363
let messages: Vec<_> = event
6464
.breadcrumbs
6565
.iter()
66-
.map(|x| {
67-
(
68-
x.message.as_ref().map(String::as_str).unwrap(),
69-
x.ty.as_str(),
70-
)
71-
})
66+
.map(|x| (x.message.as_deref().unwrap(), x.ty.as_str()))
7267
.collect();
7368
assert_eq!(
7469
messages,

0 commit comments

Comments
 (0)