From 1e523502eb02fd62ca8e85ca928f56187375d0f8 Mon Sep 17 00:00:00 2001 From: Johannes Heuel Date: Thu, 4 Jul 2024 21:31:40 +0200 Subject: [PATCH] add timeouts to http requests --- src/commands/play.rs | 8 ++++++-- src/main.rs | 8 +++++++- src/state.rs | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/commands/play.rs b/src/commands/play.rs index a8a22b4..0eb82c8 100644 --- a/src/commands/play.rs +++ b/src/commands/play.rs @@ -341,13 +341,17 @@ pub(crate) async fn play( .or(yttrack.url.clone()) .ok_or("Could not find url")?; - let mut src = YoutubeDl::new(reqwest::Client::new(), url.clone()); + let mut src = YoutubeDl::new(state.client.clone(), url.clone()); let track: Track = src.clone().into(); if let Ok(metadata) = src.aux_metadata().await { debug!("metadata: {:?}", metadata); - persistence(&interaction, yttrack, Arc::clone(&state)).await?; + persistence(&interaction, yttrack, Arc::clone(&state)) + .await + .unwrap_or_else(|e| { + tracing::error!("could not persist track: {:?}", e); + }); tracks_added.push(TrackType { url: url.clone(), diff --git a/src/main.rs b/src/main.rs index 705a135..e00a23f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,7 @@ use futures::StreamExt; use signal::signal_handler; use songbird::{shards::TwilightMap, Songbird}; use state::StateRef; -use std::{env, error::Error, str::FromStr, sync::Arc}; +use std::{env, error::Error, str::FromStr, sync::Arc, time::Duration}; use tokio::select; use tracing::{debug, info}; use twilight_cache_inmemory::InMemoryCache; @@ -82,6 +82,11 @@ async fn main() -> Result<(), Box> { ); let songbird = Songbird::twilight(Arc::new(senders), user_id); let cache = InMemoryCache::new(); + let client = reqwest::ClientBuilder::new() + .connect_timeout(Duration::from_secs(10)) + .timeout(Duration::from_secs(3600)) + .build() + .expect("could not build http client"); ( shards, @@ -92,6 +97,7 @@ async fn main() -> Result<(), Box> { standby: Standby::new(), guild_settings: Default::default(), pool, + client, }), ) }; diff --git a/src/state.rs b/src/state.rs index 4ed17ca..68e1f0c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -33,4 +33,5 @@ pub(crate) struct StateRef { pub(crate) standby: Standby, pub(crate) guild_settings: DashMap, Settings>, pub(crate) pool: sqlx::SqlitePool, + pub(crate) client: reqwest::Client, }