Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

bigtable: remove extra clones #34239

Merged
merged 1 commit into from
Dec 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions storage-bigtable/src/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,24 @@ fn load_stringified_credentials(credential: String) -> Result<Credentials, Strin
Credentials::from_str(&credential).map_err(|err| format!("{err}"))
}

#[derive(Clone)]
pub struct AccessToken {
pub struct AccessTokenInner {
credentials: Credentials,
scope: Scope,
refresh_active: Arc<AtomicBool>,
token: Arc<RwLock<(Token, Instant)>>,
token: RwLock<(Token, Instant)>,
refresh_active: AtomicBool,
}

#[derive(Clone)]
pub struct AccessToken {
inner: Arc<AccessTokenInner>,
}

impl std::ops::Deref for AccessToken {
type Target = AccessTokenInner;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl AccessToken {
Expand All @@ -52,12 +64,14 @@ impl AccessToken {
if let Err(err) = credentials.rsa_key() {
Err(format!("Invalid rsa key: {err}"))
} else {
let token = Arc::new(RwLock::new(Self::get_token(&credentials, &scope).await?));
let token = RwLock::new(Self::get_token(&credentials, &scope).await?);
let access_token = Self {
credentials,
scope,
token,
refresh_active: Arc::new(AtomicBool::new(false)),
inner: Arc::new(AccessTokenInner {
credentials,
scope,
token,
refresh_active: AtomicBool::new(false),
}),
};
Ok(access_token)
}
Expand Down Expand Up @@ -109,20 +123,17 @@ impl AccessToken {
return;
}

let credentials = self.credentials.clone();
let scope = self.scope.clone();
let refresh_active = Arc::clone(&self.refresh_active);
let token = Arc::clone(&self.token);
let this = self.clone();
tokio::spawn(async move {
match time::timeout(
time::Duration::from_secs(5),
Self::get_token(&credentials, &scope),
Self::get_token(&this.credentials, &this.scope),
)
.await
{
Ok(new_token) => match new_token {
Ok(new_token) => {
let mut token_w = token.write().unwrap();
let mut token_w = this.token.write().unwrap();
*token_w = new_token;
}
Err(err) => error!("Failed to fetch new token: {}", err),
Expand All @@ -131,7 +142,7 @@ impl AccessToken {
warn!("Token refresh timeout")
}
}
refresh_active.store(false, Ordering::Relaxed);
this.refresh_active.store(false, Ordering::Relaxed);
info!("Token refreshed");
});
}
Expand Down