implement sleep option to prevent server abuse

This commit is contained in:
yggverse 2026-04-06 22:42:40 +03:00
parent 2ea58cc9c3
commit e462665342
3 changed files with 17 additions and 5 deletions

View file

@ -1,6 +1,10 @@
# Persist processed entries between sessions # Persist processed entries between sessions
database = "database.redb" database = "database.redb"
# Iterator delay in seconds (prevents server abuse)
# * unset or comment to run asap
sleep = 1
# Update channels queue, in seconds (activates daemon mode) # Update channels queue, in seconds (activates daemon mode)
# * unset or comment to run once # * unset or comment to run once
# update = 60 # update = 60

View file

@ -12,4 +12,7 @@ pub struct Config {
/// Repeat delay in seconds (activates daemon mode) /// Repeat delay in seconds (activates daemon mode)
/// * None to run once /// * None to run once
pub update: Option<u64>, pub update: Option<u64>,
/// Iterator delay in seconds (prevents server abuse)
/// * None to run asap
pub sleep: Option<u64>,
} }

View file

@ -9,7 +9,7 @@ use config::Config;
use database::Database; use database::Database;
use log::*; use log::*;
use rustypipe::client::RustyPipe; use rustypipe::client::RustyPipe;
use std::{env::var, process::Command, time::Duration}; use std::{env::var, process::Command, thread, time::Duration};
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
@ -31,6 +31,7 @@ async fn main() {
let config: Config = let config: Config =
toml::from_str(&std::fs::read_to_string(&argument.config).unwrap()).unwrap(); toml::from_str(&std::fs::read_to_string(&argument.config).unwrap()).unwrap();
let update = config.update.map(Duration::from_secs); let update = config.update.map(Duration::from_secs);
let sleep = config.sleep.map(Duration::from_secs);
let mut database = Database::new(&config.database).unwrap(); let mut database = Database::new(&config.database).unwrap();
let rp = RustyPipe::new(); let rp = RustyPipe::new();
@ -79,7 +80,7 @@ async fn main() {
}) { }) {
match database.process(&item.id) { match database.process(&item.id) {
Ok(()) => debug!( Ok(()) => debug!(
"command `{cmd}` for channel `{c}` successful: `{:?}`", "exec `{cmd}` for channel `{c}` successful: `{:?}`",
response response
), ),
Err(e) => error!( Err(e) => error!(
@ -89,13 +90,13 @@ async fn main() {
} }
} else { } else {
warn!( warn!(
"unexpected command `{cmd}` for channel `{c}`: `{:?}`", "unexpected exec `{cmd}` for channel `{c}`: `{:?}`",
response response
) )
} }
} else { } else {
warn!( warn!(
"command `{cmd}` for channel `{c}` failed: `{:?}`", "exec `{cmd}` for channel `{c}` failed: `{:?}`",
response response
) )
} }
@ -105,6 +106,10 @@ async fn main() {
} }
} }
} }
if let Some(duration) = sleep {
debug!("await {} seconds to continue...", duration.as_secs());
thread::sleep(duration)
}
} }
} }
Err(e) => warn!("can't scrape channel `{c}`: `{e}`"), Err(e) => warn!("can't scrape channel `{c}`: `{e}`"),
@ -116,7 +121,7 @@ async fn main() {
"queue completed; await {} seconds to continue...", "queue completed; await {} seconds to continue...",
duration.as_secs() duration.as_secs()
); );
std::thread::sleep(duration) thread::sleep(duration)
} }
None => { None => {
debug!("all tasks completed; exit."); debug!("all tasks completed; exit.");