From 0eb1268567c173fc7b4e2398357a024edd3295bf Mon Sep 17 00:00:00 2001 From: Lewin Bormann Date: Thu, 13 Jun 2019 18:41:09 +0200 Subject: [PATCH] doc(tokio): Set keep_alive to false on hyper clients. This prevents hanging event loops. --- examples/test-device/src/main.rs | 5 +++-- examples/test-installed/src/main.rs | 7 ++++--- examples/test-svc-acct/src/main.rs | 9 +++++---- src/authenticator.rs | 4 ++++ 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/examples/test-device/src/main.rs b/examples/test-device/src/main.rs index f24068e..c24a032 100644 --- a/examples/test-device/src/main.rs +++ b/examples/test-device/src/main.rs @@ -11,8 +11,9 @@ fn main() { let creds = yup_oauth2::read_application_secret(path::Path::new("clientsecret.json")) .expect("clientsecret"); let https = HttpsConnector::new(1).expect("tls"); - let client = Client::builder().build::<_, hyper::Body>(https); - + let client = Client::builder() + .keep_alive(false) + .build::<_, hyper::Body>(https); let scopes = &["https://www.googleapis.com/auth/youtube.readonly".to_string()]; let ad = yup_oauth2::DefaultFlowDelegate; diff --git a/examples/test-installed/src/main.rs b/examples/test-installed/src/main.rs index 18a2440..d11df4e 100644 --- a/examples/test-installed/src/main.rs +++ b/examples/test-installed/src/main.rs @@ -9,7 +9,9 @@ use std::path::Path; fn main() { let https = HttpsConnector::new(1).expect("tls"); - let client = Client::builder().build::<_, hyper::Body>(https); + let client = Client::builder() + .keep_alive(false) + .build::<_, hyper::Body>(https); let ad = yup_oauth2::DefaultFlowDelegate; let secret = yup_oauth2::read_application_secret(Path::new("clientsecret.json")) .expect("clientsecret.json"); @@ -35,6 +37,5 @@ fn main() { Ok(()) }); - let mut rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(fut).unwrap(); + tokio::run(fut) } diff --git a/examples/test-svc-acct/src/main.rs b/examples/test-svc-acct/src/main.rs index b3fd150..f9a9ee0 100644 --- a/examples/test-svc-acct/src/main.rs +++ b/examples/test-svc-acct/src/main.rs @@ -13,7 +13,9 @@ fn main() { let creds = yup_oauth2::service_account_key_from_file(path::Path::new("serviceaccount.json")).unwrap(); let https = HttpsConnector::new(1).expect("tls"); - let client = Client::builder().build::<_, hyper::Body>(https); + let client = Client::builder() + .keep_alive(false) + .build::<_, hyper::Body>(https); let mut sa = yup_oauth2::ServiceAccountAccess::new(creds, client); @@ -29,7 +31,6 @@ fn main() { println!("cached token is {:?} and should be identical", tok); Ok(()) }); - let all = fut.join(fut2); - let mut rt = tokio::runtime::Runtime::new().unwrap(); - rt.block_on(all).unwrap(); + let all = fut.join(fut2).then(|_| Ok(())); + tokio::run(all) } diff --git a/src/authenticator.rs b/src/authenticator.rs index 557cdf3..cc166a8 100644 --- a/src/authenticator.rs +++ b/src/authenticator.rs @@ -16,6 +16,10 @@ use std::sync::{Arc, Mutex}; /// /// `ServiceAccountAccess` does not need (and does not work) with `Authenticator`, given that it /// does not require interaction and implements its own caching. Use it directly. +/// +/// NOTE: It is recommended to use a client constructed like this in order to prevent functions +/// like `hyper::run()` from hanging: `let client = hyper::Client::builder().keep_alive(false);`. +/// Due to token requests being rare, this should not result in a too bad performance problem. pub struct Authenticator< T: GetToken, S: TokenStorage,