From 33c876e3ab307aba09a06241a04bc9f379573075 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Tue, 24 Sep 2024 20:59:06 -0400 Subject: [PATCH 1/2] fix(client): add a timeout and retry logic to oauth daemon --- src/oauth.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/oauth.rs b/src/oauth.rs index efdf41e7..ffdc0850 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -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"; @@ -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, 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(); From da717c61e93d90ff6487787f5ae5835333c5fa98 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Tue, 24 Sep 2024 21:00:37 -0400 Subject: [PATCH 2/2] fix(client): add a timeout and retry logic to oauth daemon --- src/oauth.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/oauth.rs b/src/oauth.rs index ffdc0850..be7437f4 100644 --- a/src/oauth.rs +++ b/src/oauth.rs @@ -34,8 +34,8 @@ impl Oauth { match attempt { Ok(Some(oauth)) => { info!("[✅] Successfully created OAuth client"); - return oauth - }, + return oauth; + } Ok(None) => { error!("Failed to create OAuth client. Retrying in 5 seconds..."); continue;