satex_load_balancer/
background.rs1use crate::load_balancer::LoadBalancer;
2use async_trait::async_trait;
3use satex_core::background::BackgroundTask;
4use std::time::{Duration, Instant};
5use tracing::error;
6
7#[async_trait]
8impl BackgroundTask for LoadBalancer {
9 async fn run(&self) {
10 const NEVER: Duration = Duration::from_secs(u32::MAX as u64);
12 let mut now = Instant::now();
13 let mut next_update = now;
15 let mut next_health_check = now;
16 loop {
17 if next_update <= now {
18 if let Err(e) = self.update().await {
19 error!("load balancer update error: {}", e);
20 }
21 next_update = now + self.update_frequency.unwrap_or(NEVER);
22 }
23
24 if next_health_check <= now {
25 self.backends
26 .run_health_check(self.health_check_parallel)
27 .await;
28 next_health_check = now + self.health_check_frequency.unwrap_or(NEVER);
29 }
30
31 if self.update_frequency.is_none() && self.health_check_frequency.is_none() {
32 return;
33 }
34 let to_wake = std::cmp::min(next_update, next_health_check);
35 tokio::time::sleep_until(to_wake.into()).await;
36 now = Instant::now();
37 }
38 }
39}