Write action throttling code

This commit is contained in:
Félix Saparelli 2021-08-22 02:54:02 +12:00
parent ce60be2ec9
commit 2812a723ff
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
1 changed files with 24 additions and 4 deletions

View File

@ -1,8 +1,8 @@
//! Processor responsible for receiving events, filtering them, and scheduling actions in response.
use std::time::Duration;
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, watch};
use tokio::{sync::{mpsc, watch}, time::timeout};
use crate::{
error::{CriticalError, RuntimeError},
@ -24,9 +24,29 @@ impl Default for WorkingData {
}
pub async fn worker(
mut working: watch::Receiver<WorkingData>,
working: watch::Receiver<WorkingData>,
errors: mpsc::Sender<RuntimeError>,
events: mpsc::Receiver<Event>,
mut events: mpsc::Receiver<Event>,
) -> Result<(), CriticalError> {
let mut last = Instant::now();
let mut set = Vec::new();
loop {
let maxtime = working.borrow().throttle;
match timeout(maxtime, events.recv()).await {
Err(_timeout) => {},
Ok(None) => break,
Ok(Some(event)) => {
set.push(event);
if last.elapsed() < working.borrow().throttle {
continue;
}
}
}
last = Instant::now();
set.drain(..); // TODO: do action with the set
}
Ok(())
}