feat: break out of subscription creation loop after 50 attempts

This commit is contained in:
Youwen Wu 2024-03-07 09:51:56 -08:00
parent 986279ad08
commit ac81787597
Signed by: youwen5
GPG key ID: 865658ED1FE61EC3

View file

@ -14,7 +14,9 @@ use tokio::time::{sleep, Duration};
/// ///
/// This function will retry creating a subscription every 3 seconds /// This function will retry creating a subscription every 3 seconds
/// if it fails. /// if it fails.
pub async fn create_subscription(client: &Client) -> Subscription { pub async fn create_subscription(client: &Client) -> Result<Subscription, network_tables::Error> {
let mut attempts: u8 = 0;
loop { loop {
let subscription_attempt = client let subscription_attempt = client
.subscribe_w_options( .subscribe_w_options(
@ -28,12 +30,17 @@ pub async fn create_subscription(client: &Client) -> Subscription {
.await; .await;
match subscription_attempt { match subscription_attempt {
Ok(subscription) => break subscription, Ok(subscription) => break Ok(subscription),
Err(e) => { Err(e) => {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
println!("Failed to create subscription: {}", e); println!("Failed to create subscription: {}", e);
} }
if attempts >= 50 {
break Err(e);
}
attempts += 1;
sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying sleep(Duration::from_secs(3)).await; // Wait for 3 seconds before retrying
continue; continue;
} }