Skip to content

Commit

Permalink
fix: get rid of unsafe code by arc-swap
Browse files Browse the repository at this point in the history
  • Loading branch information
thynson committed Jul 10, 2024
1 parent 86f69f4 commit ede73a3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ tls = ["reqwest/default-tls"]
auth-by-http = ["reqwest"]

[dependencies]
arc-swap = "1.7"
nacos-macro = { version = "0.1.0", path = "nacos-macro" }
thiserror = "1.0"
tokio = { version = "1", features = ["full"] }
Expand Down
33 changes: 17 additions & 16 deletions src/api/plugin/auth/auth_by_http.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use arc_swap::ArcSwap;
use rand::Rng;
use std::ops::Add;
use std::ops::{Add, Deref};
use std::sync::Arc;
use tokio::time::{Duration, Instant};

use crate::api::plugin::{AuthContext, AuthPlugin, LoginIdentityContext};
Expand All @@ -15,15 +17,15 @@ pub(crate) const TOKEN_TTL: &str = "tokenTtl";

/// Http login AuthPlugin.
pub struct HttpLoginAuthPlugin {
login_identity: LoginIdentityContext,
next_login_refresh: Instant,
login_identity: ArcSwap<LoginIdentityContext>,
next_login_refresh: ArcSwap<Instant>,
}

impl Default for HttpLoginAuthPlugin {
fn default() -> Self {
Self {
login_identity: LoginIdentityContext::default(),
next_login_refresh: Instant::now(),
login_identity: ArcSwap::from_pointee(LoginIdentityContext::default()),
next_login_refresh: ArcSwap::from_pointee(Instant::now()),
}
}
}
Expand All @@ -32,7 +34,7 @@ impl Default for HttpLoginAuthPlugin {
impl AuthPlugin for HttpLoginAuthPlugin {
async fn login(&self, server_list: Vec<String>, auth_context: AuthContext) {
let now_instant = Instant::now();
if now_instant.le(&self.next_login_refresh) {
if now_instant.le(self.next_login_refresh.load().deref()) {
tracing::debug!("Http login return because now_instant lte next_login_refresh.");
return;
}
Expand Down Expand Up @@ -86,20 +88,19 @@ impl AuthPlugin for HttpLoginAuthPlugin {

if let Some(login_response) = login_response {
let delay_sec = login_response.token_ttl / 10;
let new_login_identity = LoginIdentityContext::default()
.add_context(ACCESS_TOKEN, login_response.access_token);

unsafe {
#[warn(invalid_reference_casting)]
let mut_self = &mut *(self as *const Self as *mut Self);
mut_self.next_login_refresh = Instant::now().add(Duration::from_secs(delay_sec));
mut_self.login_identity = new_login_identity;
}
let new_login_identity = Arc::new(
LoginIdentityContext::default()
.add_context(ACCESS_TOKEN, login_response.access_token),
);
self.login_identity.store(new_login_identity);

self.next_login_refresh
.store(Arc::new(Instant::now().add(Duration::from_secs(delay_sec))));
}
}

fn get_login_identity(&self) -> LoginIdentityContext {
self.login_identity.to_owned()
self.login_identity.load().deref().deref().to_owned()
}
}

Expand Down

0 comments on commit ede73a3

Please sign in to comment.