pingora_core/services/
mod.rs

1// Copyright 2024 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//! The service interface
16//!
17//! A service to the pingora server is just something runs forever until the server is shutting
18//! down.
19//!
20//! Two types of services are particularly useful
21//! - services that are listening to some (TCP) endpoints
22//! - services that are just running in the background.
23
24use async_trait::async_trait;
25
26#[cfg(unix)]
27use crate::server::ListenFds;
28use crate::server::ShutdownWatch;
29
30pub mod background;
31pub mod listening;
32
33/// The service interface
34#[async_trait]
35pub trait Service: Sync + Send {
36    /// This function will be called when the server is ready to start the service.
37    ///
38    /// - `fds` (Unix only): a collection of listening file descriptors. During zero downtime restart
39    /// the `fds` would contain the listening sockets passed from the old service, services should
40    /// take the sockets they need to use then. If the sockets the service looks for don't appear in
41    /// the collection, the service should create its own listening sockets and then put them into
42    /// the collection in order for them to be passed to the next server.
43    /// - `shutdown`: the shutdown signal this server would receive.
44    async fn start_service(
45        &mut self,
46        #[cfg(unix)] fds: Option<ListenFds>,
47        mut shutdown: ShutdownWatch,
48    );
49
50    /// The name of the service, just for logging and naming the threads assigned to this service
51    ///
52    /// Note that due to the limit of the underlying system, only the first 16 chars will be used
53    fn name(&self) -> &str;
54
55    /// The preferred number of threads to run this service
56    ///
57    /// If `None`, the global setting will be used
58    fn threads(&self) -> Option<usize> {
59        None
60    }
61}