Skip to main content

pingora_load_balancing/
background.rs

1// Copyright 2026 Cloudflare, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Implement [BackgroundService] for [LoadBalancer]
16
17use std::time::{Duration, Instant};
18
19use super::{BackendIter, BackendSelection, LoadBalancer};
20use async_trait::async_trait;
21use pingora_core::services::{background::BackgroundService, ServiceReadyNotifier};
22
23impl<S: Send + Sync + BackendSelection + 'static> LoadBalancer<S>
24where
25    S::Iter: BackendIter,
26{
27    pub async fn run(
28        &self,
29        shutdown: pingora_core::server::ShutdownWatch,
30        mut ready_opt: Option<ServiceReadyNotifier>,
31    ) -> () {
32        // 136 years
33        const NEVER: Duration = Duration::from_secs(u32::MAX as u64);
34        let mut now = Instant::now();
35        // run update and health check once
36        let mut next_update = now;
37        let mut next_health_check = now;
38
39        loop {
40            if *shutdown.borrow() {
41                return;
42            }
43
44            if next_update <= now {
45                // TODO: log err
46                let _ = self.update().await;
47                next_update = now + self.update_frequency.unwrap_or(NEVER);
48            }
49
50            // After the first update, discovery and selection setup will be
51            // done, so we will notify dependents
52            if let Some(ready) = ready_opt.take() {
53                ServiceReadyNotifier::notify_ready(ready)
54            }
55
56            if next_health_check <= now {
57                self.backends
58                    .run_health_check(self.parallel_health_check)
59                    .await;
60                next_health_check = now + self.health_check_frequency.unwrap_or(NEVER);
61            }
62
63            if self.update_frequency.is_none() && self.health_check_frequency.is_none() {
64                return;
65            }
66            let to_wake = std::cmp::min(next_update, next_health_check);
67            tokio::time::sleep_until(to_wake.into()).await;
68            now = Instant::now();
69        }
70    }
71}
72
73/// Implement [BackgroundService] for [LoadBalancer]. For backward-compatibility
74/// reasons, we implement both the `start` and `start_with_ready_notifier`
75/// methods.
76#[async_trait]
77impl<S: Send + Sync + BackendSelection + 'static> BackgroundService for LoadBalancer<S>
78where
79    S::Iter: BackendIter,
80{
81    async fn start_with_ready_notifier(
82        &self,
83        shutdown: pingora_core::server::ShutdownWatch,
84        ready: ServiceReadyNotifier,
85    ) -> () {
86        self.run(shutdown, Some(ready)).await
87    }
88
89    async fn start(&self, shutdown: pingora_core::server::ShutdownWatch) -> () {
90        self.run(shutdown, None).await
91    }
92}