Skip to main content

codex_convert_proxy/
server.rs

1//! Pingora server startup and configuration.
2//!
3//! This module handles the initialization and startup of the pingora proxy server.
4
5use pingora_core::server::configuration::ServerConf;
6use pingora_core::server::Server;
7use pingora_proxy::http_proxy_service;
8use tracing::info;
9
10use crate::proxy::CodexProxy;
11
12/// Graceful shutdown timeout in seconds (wait for existing requests to complete)
13const GRACEFUL_SHUTDOWN_TIMEOUT: u64 = 30;
14
15/// Grace period before starting final shutdown step
16const GRACEFUL_PERIOD: u64 = 10;
17
18/// Start the pingora proxy server.
19///
20/// This function bootstraps the pingora server, adds the CodexProxy service,
21/// and runs it forever. The server handles graceful shutdown automatically
22/// (SIGTERM for graceful, SIGINT for fast shutdown).
23pub fn start_proxy_server(
24    proxy: CodexProxy,
25    listen: &str,
26) {
27    // Create server configuration with custom graceful shutdown settings
28    let mut server_conf = ServerConf::new().expect("Failed to create server config");
29    server_conf.grace_period_seconds = Some(GRACEFUL_PERIOD);
30    server_conf.graceful_shutdown_timeout_seconds = Some(GRACEFUL_SHUTDOWN_TIMEOUT);
31
32    let mut server = Server::new_with_opt_and_conf(None, server_conf);
33
34    let mut http_proxy = http_proxy_service(&server.configuration, proxy);
35    http_proxy.add_tcp(listen);
36
37    server.add_service(http_proxy);
38
39    info!("Server listening on {}", listen);
40    info!("Graceful shutdown: {}s grace period, {}s timeout",
41          GRACEFUL_PERIOD, GRACEFUL_SHUTDOWN_TIMEOUT);
42    server.run_forever();
43}