Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get rid of unsafe code by arc-swap #234

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
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
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) {
thynson marked this conversation as resolved.
Show resolved Hide resolved
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()
thynson marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Loading