Skip to content

Commit

Permalink
Fix panic when the irc client is created without an internet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Nogesma committed May 20, 2023
1 parent f32b822 commit e2df0db
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
48 changes: 41 additions & 7 deletions src/twitch/connection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::min;
use std::time::Duration;

use irc::{
Expand All @@ -12,7 +13,7 @@ use crate::handlers::{
};

/// Initialize the config and send it to the client to connect to an IRC channel.
pub async fn create_client_stream(config: CompleteConfig) -> (Client, ClientStream) {
async fn create_client_stream(config: CompleteConfig) -> Result<(Client, ClientStream), Error> {
let irc_config = Config {
nickname: Some(config.twitch.username.clone()),
server: Some(config.twitch.server.clone()),
Expand All @@ -25,13 +26,42 @@ pub async fn create_client_stream(config: CompleteConfig) -> (Client, ClientStre
..Default::default()
};

let mut client = Client::from_config(irc_config.clone()).await.unwrap();
let mut client = Client::from_config(irc_config.clone()).await?;

client.identify().unwrap();
client.identify()?;

let stream = client.stream().unwrap();
let stream = client.stream()?;

(client, stream)
Ok((client, stream))
}

pub async fn wait_client_stream(
tx: Sender<MessageData>,
data_builder: DataBuilder<'_>,
config: CompleteConfig,
) -> (Client, ClientStream) {
let mut timeout = 1;

loop {
match create_client_stream(config.clone()).await {
Ok(v) => return v,
Err(err) => match err {
Error::Io(io) => tx
.send(data_builder.system(format!("Unable to connect: {io}")))
.await
.unwrap(),
_ => {
tx.send(data_builder.system(format!("Fatal error: {err:?}").to_string()))
.await
.unwrap();
}
},
};

sleep(Duration::from_secs(timeout)).await;

timeout = min(timeout * 2, 30);
}
}

/// If an error of any kind occurs, attempt to reconnect to the IRC channel.
Expand All @@ -54,9 +84,13 @@ pub async fn client_stream_reconnect(
}
}

let (client, stream) = create_client_stream(config.clone()).await;

sleep(Duration::from_millis(1000)).await;

tx.send(data_builder.system("Attempting reconnect...".to_string()))
.await
.unwrap();

let (client, stream) = wait_client_stream(tx, data_builder, config.clone()).await;

(client, stream)
}
6 changes: 3 additions & 3 deletions src/twitch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
},
twitch::{
badges::retrieve_user_badges,
connection::{client_stream_reconnect, create_client_stream},
connection::{client_stream_reconnect, wait_client_stream},
},
};

Expand Down Expand Up @@ -54,7 +54,8 @@ pub async fn twitch_irc(
let data_builder = DataBuilder::new(&config.frontend.date_format);
let mut room_state_startup = false;

let (mut client, mut stream) = create_client_stream(config.clone()).await;
let (mut client, mut stream) =
wait_client_stream(tx.clone(), data_builder, config.clone()).await;

let sender = client.sender();

Expand Down Expand Up @@ -132,7 +133,6 @@ pub async fn twitch_irc(

(client, stream) = client_stream_reconnect(err, tx.clone(), data_builder, &config).await;

tx.send(data_builder.system("Attempting reconnect...".to_string())).await.unwrap();
}
}
}
Expand Down

0 comments on commit e2df0db

Please sign in to comment.