Patches to get deadlocking in osu updates fixed

This commit is contained in:
Natsu Kagami 2023-11-09 01:36:58 +01:00
parent 1b02993e98
commit 220dbc21ea
Signed by: nki
GPG key ID: 55A032EB38B49ADB
7 changed files with 60 additions and 41 deletions

View file

@ -19,6 +19,7 @@ use serenity::{
CacheAndHttp,
};
use std::{collections::HashMap, sync::Arc};
use tokio::time::{interval, MissedTickBehavior};
use youmubot_db::DB;
/// A list of assigned channels for an announcer.
@ -149,28 +150,32 @@ impl AnnouncerHandler {
/// Start the AnnouncerHandler, looping forever.
///
/// It will run all the announcers in sequence every *cooldown* seconds.
/// It will run all the announcers every *cooldown* seconds.
pub async fn scan(self, cooldown: std::time::Duration) {
// First we store all the keys inside the database.
let keys = self.announcers.keys().cloned().collect::<Vec<_>>();
self.data.write().await.insert::<Self>(keys.clone());
loop {
eprintln!("{}: announcer started scanning", chrono::Utc::now());
let after = tokio::time::sleep_until(tokio::time::Instant::now() + cooldown);
join_all(self.announcers.iter().map(|(key, announcer)| {
eprintln!(" - scanning key `{}`", key);
Self::announce(self.data.clone(), self.cache_http.clone(), key, announcer).map(
move |v| {
if let Err(e) = v {
join_all(self.announcers.iter().map(|(key, announcer)| {
let data = self.data.clone();
let cache = self.cache_http.clone();
let mut looper = interval(cooldown);
looper.set_missed_tick_behavior(MissedTickBehavior::Delay);
async move {
loop {
eprintln!(" - scanning key `{}`", key);
match Self::announce(data.clone(), cache.clone(), key, announcer).await {
Err(e) => {
eprintln!(" - key `{}`: {:?}", *key, e)
}
},
)
}))
.await;
eprintln!("{}: announcer finished scanning", chrono::Utc::now());
after.await;
}
Ok(()) => {
eprintln!(" - key `{}`: complete", *key)
}
};
looper.tick().await;
}
}
}))
.await;
}
}

View file

@ -1,5 +1,5 @@
use serenity::prelude::*;
use std::path::Path;
use std::{path::Path, time::Duration};
/// Set up the prelude libraries.
///
@ -22,7 +22,13 @@ pub async fn setup_prelude(
.expect("SQL database set up");
// Set up the HTTP client.
data.insert::<crate::HTTPClient>(reqwest::Client::new());
data.insert::<crate::HTTPClient>(
reqwest::ClientBuilder::new()
.connect_timeout(Duration::from_secs(5))
.timeout(Duration::from_secs(60))
.build()
.expect("Build be able to build HTTP client"),
);
// Set up the member cache.
data.insert::<crate::MemberCache>(std::sync::Arc::new(crate::MemberCache::default()));