From a5cdf075ed28e43fb2fa859f8ad2dfbfdab50f13 Mon Sep 17 00:00:00 2001 From: Brandon Roberts <106782975+brandon-j-roberts@users.noreply.github.com> Date: Mon, 14 Nov 2022 17:39:26 -0600 Subject: [PATCH] bigtable: add timeout to token refresh (#28728) Co-authored-by: Kirill Fomichev (cherry picked from commit 55985701ba0491a8db4b1c570d888c0a23f09515) --- storage-bigtable/src/access_token.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/storage-bigtable/src/access_token.rs b/storage-bigtable/src/access_token.rs index 3c2d596cf07081..c6664ba887fa7d 100644 --- a/storage-bigtable/src/access_token.rs +++ b/storage-bigtable/src/access_token.rs @@ -16,6 +16,7 @@ use { }, time::Instant, }, + tokio::time, }; fn load_credentials(filepath: Option) -> Result { @@ -109,15 +110,22 @@ impl AccessToken { } info!("Refreshing token"); - let new_token = Self::get_token(&self.credentials, &self.scope).await; + match time::timeout( + time::Duration::from_secs(5), + Self::get_token(&self.credentials, &self.scope), + ) + .await { - let mut token_w = self.token.write().unwrap(); - match new_token { - Ok(new_token) => *token_w = new_token, - Err(err) => warn!("{}", err), + Ok(new_token) => match (new_token, self.token.write()) { + (Ok(new_token), Ok(mut token_w)) => *token_w = new_token, + (Ok(_new_token), Err(err)) => warn!("{}", err), + (Err(err), _) => warn!("{}", err), + }, + Err(_) => { + warn!("Token refresh timeout") } - self.refresh_active.store(false, Ordering::Relaxed); } + self.refresh_active.store(false, Ordering::Relaxed); } /// Return an access token suitable for use in an HTTP authorization header