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(client): add a timeout and retry logic to oauth daemon #256

Merged
merged 2 commits into from
Sep 25, 2024
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
28 changes: 25 additions & 3 deletions src/oauth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use crate::{
};
use base64::{engine::general_purpose, Engine as _};
use hyper::{client, Body, Method, Request};
use log::{info, trace};
use log::{error, info, trace};

use serde_json::json;
use tokio::time::{error::Elapsed, timeout};

static REDDIT_ANDROID_OAUTH_CLIENT_ID: &str = "ohXpoqrZYub1kg";

Expand All @@ -25,11 +26,32 @@ pub struct Oauth {
}

impl Oauth {
/// Create a new OAuth client
pub(crate) async fn new() -> Self {
// Call new_internal until it succeeds
loop {
let attempt = Self::new_with_timeout().await;
match attempt {
Ok(Some(oauth)) => {
info!("[✅] Successfully created OAuth client");
return oauth;
}
Ok(None) => {
error!("Failed to create OAuth client. Retrying in 5 seconds...");
continue;
}
Err(duration) => {
error!("Failed to create OAuth client in {duration:?}. Retrying in 5 seconds...");
}
}
}
}

async fn new_with_timeout() -> Result<Option<Self>, Elapsed> {
let mut oauth = Self::default();
oauth.login().await;
oauth
timeout(Duration::from_secs(5), oauth.login()).await.map(|result| result.map(|_| oauth))
}

pub(crate) fn default() -> Self {
// Generate a device to spoof
let device = Device::new();
Expand Down
Loading