hunter/scheduler: add jitter to interval on no new posts

This commit is contained in:
Astro 2022-11-07 04:08:31 +01:00
parent 566aecc3dd
commit e9d3c9aed0
3 changed files with 54 additions and 2 deletions

48
Cargo.lock generated
View File

@ -267,6 +267,7 @@ dependencies = [
"cave",
"chrono",
"log",
"rand",
"redis",
"reqwest",
"serde",
@ -592,6 +593,17 @@ dependencies = [
"slab",
]
[[package]]
name = "getrandom"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
dependencies = [
"cfg-if",
"libc",
"wasi 0.11.0+wasi-snapshot-preview1",
]
[[package]]
name = "h2"
version = "0.3.15"
@ -1076,6 +1088,12 @@ version = "0.3.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ac9a59f73473f1b8d852421e59e64809f025994837ef743615c6d0c5b305160"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "proc-macro2"
version = "1.0.47"
@ -1094,6 +1112,36 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "redis"
version = "0.22.1"

View File

@ -13,3 +13,4 @@ redis = { version = "0.22", features = ["tokio-comp", "connection-manager"] }
log = "0.4"
systemd = "0.10"
cave = { path = "../cave" }
rand = "0.8"

View File

@ -1,5 +1,6 @@
use std::collections::{HashMap, BTreeMap};
use std::time::Duration;
use rand::{thread_rng, Rng};
use tokio::time::Instant;
const MIN_INTERVAL: Duration = Duration::from_secs(30);
@ -56,8 +57,10 @@ impl Scheduler {
let next_interval = match (new_post_ratio, mean_interval, last_interval) {
(Some(new_post_ratio), Some(mean_interval), _) if new_post_ratio > 0. =>
mean_interval,
(_, _, Some(last_interval)) =>
2 * last_interval,
(_, _, Some(last_interval)) => {
let a = thread_rng().gen_range(2. .. 3.);
last_interval.mul_f64(a)
}
_ =>
DEFAULT_INTERVAL,
}.max(MIN_INTERVAL).min(MAX_INTERVAL);