From e4626653421aaf741ac437dc07ad3c574f711722 Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 6 Apr 2026 22:42:40 +0300 Subject: [PATCH] implement sleep option to prevent server abuse --- example/config.toml | 4 ++++ src/config.rs | 3 +++ src/main.rs | 15 ++++++++++----- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/example/config.toml b/example/config.toml index 2f515d7..5c06d27 100644 --- a/example/config.toml +++ b/example/config.toml @@ -1,6 +1,10 @@ # Persist processed entries between sessions 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) # * unset or comment to run once # update = 60 diff --git a/src/config.rs b/src/config.rs index 5bdc140..654cf57 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,4 +12,7 @@ pub struct Config { /// Repeat delay in seconds (activates daemon mode) /// * None to run once pub update: Option, + /// Iterator delay in seconds (prevents server abuse) + /// * None to run asap + pub sleep: Option, } diff --git a/src/main.rs b/src/main.rs index b991bd1..3bed63a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ use config::Config; use database::Database; use log::*; use rustypipe::client::RustyPipe; -use std::{env::var, process::Command, time::Duration}; +use std::{env::var, process::Command, thread, time::Duration}; #[tokio::main] async fn main() { @@ -31,6 +31,7 @@ async fn main() { let config: Config = toml::from_str(&std::fs::read_to_string(&argument.config).unwrap()).unwrap(); 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 rp = RustyPipe::new(); @@ -79,7 +80,7 @@ async fn main() { }) { match database.process(&item.id) { Ok(()) => debug!( - "command `{cmd}` for channel `{c}` successful: `{:?}`", + "exec `{cmd}` for channel `{c}` successful: `{:?}`", response ), Err(e) => error!( @@ -89,13 +90,13 @@ async fn main() { } } else { warn!( - "unexpected command `{cmd}` for channel `{c}`: `{:?}`", + "unexpected exec `{cmd}` for channel `{c}`: `{:?}`", response ) } } else { warn!( - "command `{cmd}` for channel `{c}` failed: `{:?}`", + "exec `{cmd}` for channel `{c}` failed: `{:?}`", 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}`"), @@ -116,7 +121,7 @@ async fn main() { "queue completed; await {} seconds to continue...", duration.as_secs() ); - std::thread::sleep(duration) + thread::sleep(duration) } None => { debug!("all tasks completed; exit.");