Skip to content

Commit

Permalink
fix(oauth): strengthen sync guarantees
Browse files Browse the repository at this point in the history
  • Loading branch information
sigaloid committed Jun 27, 2024
1 parent 8c5aaaa commit 3bd8b51
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use percent_encoding::{percent_encode, CONTROLS};
use serde_json::Value;

use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicU16, Ordering::Relaxed};
use std::sync::atomic::{AtomicU16, Ordering::SeqCst};
use std::{io, result::Result};
use tokio::sync::RwLock;

Expand Down Expand Up @@ -317,10 +317,10 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
};

// First, handle rolling over the OAUTH_CLIENT if need be.
let current_rate_limit = OAUTH_RATELIMIT_REMAINING.load(Ordering::Relaxed);
let current_rate_limit = OAUTH_RATELIMIT_REMAINING.load(Ordering::SeqCst);
if current_rate_limit < 10 {
warn!("Rate limit {current_rate_limit} is low. Spawning force_refresh_token()");
OAUTH_RATELIMIT_REMAINING.store(99, Ordering::Relaxed);
OAUTH_RATELIMIT_REMAINING.store(99, Ordering::SeqCst);
tokio::spawn(force_refresh_token());
}

Expand All @@ -333,7 +333,7 @@ pub async fn json(path: String, quarantine: bool) -> Result<Value, String> {
if let Some(Ok(remaining)) = response.headers().get("x-ratelimit-remaining").map(|val| val.to_str()) {
trace!("Ratelimit remaining: {}", remaining);
if let Ok(remaining) = remaining.parse::<f32>().map(|f| f.round() as u16) {
OAUTH_RATELIMIT_REMAINING.store(remaining, Relaxed);
OAUTH_RATELIMIT_REMAINING.store(remaining, SeqCst);
} else {
warn!("Failed to parse rate limit {remaining} from header.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ pub async fn token_daemon() {
}

pub async fn force_refresh_token() {
trace!("Rolling over refresh token. Current rate limit: {}", OAUTH_RATELIMIT_REMAINING.load(Ordering::Relaxed));
trace!("Rolling over refresh token. Current rate limit: {}", OAUTH_RATELIMIT_REMAINING.load(Ordering::SeqCst));
OAUTH_CLIENT.write().await.refresh().await;
}

Expand Down

0 comments on commit 3bd8b51

Please sign in to comment.