Tune announcer performance

This commit is contained in:
Natsu Kagami 2020-02-11 19:44:10 -05:00
parent 063c8baaa9
commit 5d6e537ac2
Signed by: nki
GPG key ID: 73376E117CD20735
7 changed files with 15 additions and 3 deletions

2
Cargo.lock generated
View file

@ -1720,6 +1720,7 @@ dependencies = [
"Inflector 0.11.4 (registry+https://github.com/rust-lang/crates.io-index)",
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"codeforces 0.1.0 (git+https://github.com/natsukagami/rust-codeforces-api)",
"crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1776,6 +1777,7 @@ dependencies = [
name = "youmubot-prelude"
version = "0.1.0"
dependencies = [
"chrono 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
"crossbeam-channel 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rayon 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"reqwest 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -15,6 +15,7 @@ regex = "1"
lazy_static = "1"
rayon = "1"
chrono = { version = "0.4", features = ["serde"] }
crossbeam-channel = "0.4"
youmubot-prelude = { path = "../youmubot-prelude" }
youmubot-db = { path = "../youmubot-db" }

View file

@ -39,6 +39,8 @@ fn update_user(
user_id: UserId,
cfu: &mut CfUser,
) -> CommandResult {
// Ensure this takes 200ms
let after = crossbeam_channel::after(std::time::Duration::from_secs_f32(0.2));
let info = User::info(reqwest, &[cfu.handle.as_str()])?
.into_iter()
.next()
@ -91,6 +93,7 @@ fn update_user(
}
}
}
after.recv().ok();
Ok(())
}

View file

@ -41,7 +41,7 @@ impl Client {
fn build_request(&self, r: RequestBuilder) -> Result<Response, Error> {
let v = r.query(&[("k", &*self.key)]).build()?;
dbg!(v.url());
// dbg!(v.url());
Ok(self.client.execute(v)?)
}

View file

@ -12,3 +12,4 @@ youmubot-db = { path = "../youmubot-db" }
crossbeam-channel = "0.4"
reqwest = "0.10"
rayon = "1"
chrono = "0.4"

View file

@ -1,4 +1,5 @@
use crate::{AppData, GetCloned};
use crossbeam_channel::after;
use rayon::prelude::*;
use serenity::{
framework::standard::{
@ -153,12 +154,16 @@ impl AnnouncerHandler {
let keys = self.announcers.keys().cloned().collect::<Vec<_>>();
self.data.write().insert::<Self>(keys.clone());
spawn(move || loop {
eprintln!("{}: announcer started scanning", chrono::Utc::now());
let after_timer = after(cooldown);
for key in &keys {
eprintln!(" - scanning key `{}`", key);
if let Err(e) = self.announce(key) {
dbg!(e);
}
}
std::thread::sleep(cooldown);
eprintln!("{}: announcer finished scanning", chrono::Utc::now());
after_timer.recv().ok();
})
}
}

View file

@ -98,7 +98,7 @@ fn main() {
println!("codeforces enabled.");
client.with_framework(fw);
announcers.scan(std::time::Duration::from_secs(300));
announcers.scan(std::time::Duration::from_secs(120));
println!("Starting...");
if let Err(v) = client.start() {