From 2be38a37597366f71f8680210582370ffa787c4a Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Tue, 21 Nov 2023 13:27:22 +0100 Subject: [PATCH] Add a timeout to the reqwest client --- youmubot-osu/src/lib.rs | 7 +++++-- youmubot-prelude/src/announcer.rs | 17 ++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/youmubot-osu/src/lib.rs b/youmubot-osu/src/lib.rs index 1e7df16..3298154 100644 --- a/youmubot-osu/src/lib.rs +++ b/youmubot-osu/src/lib.rs @@ -9,7 +9,7 @@ use models::*; use request::builders::*; use request::*; use reqwest::Client as HTTPClient; -use std::convert::TryInto; +use std::{convert::TryInto, time::Duration}; use youmubot_prelude::{ratelimit::Ratelimit, *}; /// The number of requests per minute to the osu! server. @@ -35,7 +35,10 @@ impl Client { /// Create a new client from the given API key. pub fn new(key: String) -> Client { let client = Ratelimit::new( - HTTPClient::new(), + HTTPClient::builder() + .timeout(Duration::from_secs(2)) + .build() + .expect("Client should be built"), REQUESTS_PER_MINUTE, std::time::Duration::from_secs(60), ); diff --git a/youmubot-prelude/src/announcer.rs b/youmubot-prelude/src/announcer.rs index ae029ef..b3b849f 100644 --- a/youmubot-prelude/src/announcer.rs +++ b/youmubot-prelude/src/announcer.rs @@ -163,15 +163,14 @@ impl AnnouncerHandler { async move { loop { eprintln!(" - scanning key `{}`", key); - Self::announce(data.clone(), cache.clone(), key, announcer) - .map(move |v| { - if let Err(e) = v { - eprintln!(" - key `{}`: {:?}", *key, e) - } else { - eprintln!(" - key `{}`: complete", *key) - } - }) - .await; + match Self::announce(data.clone(), cache.clone(), key, announcer).await { + Err(e) => { + eprintln!(" - key `{}`: {:?}", *key, e) + } + Ok(()) => { + eprintln!(" - key `{}`: complete", *key) + } + }; looper.tick().await; } }