|
| 1 | +#[cfg(feature = "metrics")] |
| 2 | +use super::metrics; |
| 3 | +use super::settings::TelemetrySettings; |
| 4 | +use crate::telemetry::log; |
| 5 | +use crate::BootstrapResult; |
| 6 | +use anyhow::Context as _; |
| 7 | +use futures_util::future::FutureExt; |
| 8 | +use futures_util::{pin_mut, ready}; |
| 9 | +use hyper_util::rt::TokioIo; |
| 10 | +use socket2::{Domain, SockAddr, Socket, Type}; |
| 11 | +use std::convert::Infallible; |
| 12 | +use std::future::Future; |
| 13 | +use std::net::SocketAddr; |
| 14 | +use std::pin::Pin; |
| 15 | +use std::sync::Arc; |
| 16 | +use std::task::{Context, Poll}; |
| 17 | +use tokio::net::TcpListener; |
| 18 | +use tokio::sync::watch; |
| 19 | + |
| 20 | +mod router; |
| 21 | + |
| 22 | +use router::Router; |
| 23 | +pub use router::{ |
| 24 | + BoxError, TelemetryRouteHandler, TelemetryRouteHandlerFuture, TelemetryServerRoute, |
| 25 | +}; |
| 26 | + |
| 27 | +pub(super) struct TelemetryServerFuture { |
| 28 | + listener: TcpListener, |
| 29 | + router: Router, |
| 30 | +} |
| 31 | + |
| 32 | +impl TelemetryServerFuture { |
| 33 | + pub(super) fn new( |
| 34 | + settings: TelemetrySettings, |
| 35 | + custom_routes: Vec<TelemetryServerRoute>, |
| 36 | + ) -> BootstrapResult<Option<TelemetryServerFuture>> { |
| 37 | + if !settings.server.enabled { |
| 38 | + return Ok(None); |
| 39 | + } |
| 40 | + |
| 41 | + let settings = Arc::new(settings); |
| 42 | + |
| 43 | + // Eagerly init the memory profiler so it gets set up before syscalls are sandboxed with seccomp. |
| 44 | + #[cfg(all(target_os = "linux", feature = "memory-profiling"))] |
| 45 | + if settings.memory_profiler.enabled { |
| 46 | + memory_profiling::profiler(Arc::clone(&settings)) |
| 47 | + .map_err(|err| anyhow::anyhow!(err))?; |
| 48 | + } |
| 49 | + |
| 50 | + let addr = settings.server.addr; |
| 51 | + |
| 52 | + #[cfg(feature = "settings")] |
| 53 | + let addr = SocketAddr::from(addr); |
| 54 | + |
| 55 | + let router = Router::new(custom_routes, settings); |
| 56 | + |
| 57 | + let listener = { |
| 58 | + let std_listener = std::net::TcpListener::from( |
| 59 | + bind_socket(addr).with_context(|| format!("binding to socket {addr:?}"))?, |
| 60 | + ); |
| 61 | + |
| 62 | + std_listener.set_nonblocking(true)?; |
| 63 | + |
| 64 | + tokio::net::TcpListener::from_std(std_listener)? |
| 65 | + }; |
| 66 | + |
| 67 | + Ok(Some(TelemetryServerFuture { listener, router })) |
| 68 | + } |
| 69 | + pub(super) fn local_addr(&self) -> SocketAddr { |
| 70 | + self.listener.local_addr().unwrap() |
| 71 | + } |
| 72 | + |
| 73 | + // Adapted from Hyper 0.14 Server stuff and axum::serve::serve. |
| 74 | + pub(super) async fn with_graceful_shutdown( |
| 75 | + self, |
| 76 | + shutdown_signal: impl Future<Output = ()> + Send + Sync + 'static, |
| 77 | + ) { |
| 78 | + let (signal_tx, signal_rx) = watch::channel(()); |
| 79 | + let signal_tx = Arc::new(signal_tx); |
| 80 | + |
| 81 | + tokio::spawn(async move { |
| 82 | + shutdown_signal.await; |
| 83 | + |
| 84 | + drop(signal_rx); |
| 85 | + }); |
| 86 | + |
| 87 | + let (close_tx, close_rx) = watch::channel(()); |
| 88 | + let listener = self.listener; |
| 89 | + |
| 90 | + pin_mut!(listener); |
| 91 | + |
| 92 | + loop { |
| 93 | + let socket = tokio::select! { |
| 94 | + conn = listener.accept() => match conn { |
| 95 | + Ok((conn, _)) => TokioIo::new(conn), |
| 96 | + Err(e) => { |
| 97 | + log::warn!("failed to accept connection"; "error" => e); |
| 98 | + |
| 99 | + continue; |
| 100 | + } |
| 101 | + }, |
| 102 | + _ = signal_tx.closed() => { break }, |
| 103 | + }; |
| 104 | + |
| 105 | + let router = self.router.clone(); |
| 106 | + let signal_tx = Arc::clone(&signal_tx); |
| 107 | + let close_rx = close_rx.clone(); |
| 108 | + |
| 109 | + tokio::spawn(async move { |
| 110 | + let conn = hyper::server::conn::http1::Builder::new() |
| 111 | + .serve_connection(socket, router) |
| 112 | + .with_upgrades(); |
| 113 | + |
| 114 | + let signal_closed = signal_tx.closed().fuse(); |
| 115 | + |
| 116 | + pin_mut!(conn); |
| 117 | + pin_mut!(signal_closed); |
| 118 | + |
| 119 | + loop { |
| 120 | + tokio::select! { |
| 121 | + _ = conn.as_mut() => break, |
| 122 | + _ = &mut signal_closed => conn.as_mut().graceful_shutdown(), |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + drop(close_rx); |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + drop(close_rx); |
| 131 | + |
| 132 | + close_tx.closed().await; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl Future for TelemetryServerFuture { |
| 137 | + type Output = Infallible; |
| 138 | + |
| 139 | + fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> { |
| 140 | + let this = &mut *self; |
| 141 | + |
| 142 | + loop { |
| 143 | + let socket = match ready!(Pin::new(&mut this.listener).poll_accept(cx)) { |
| 144 | + Ok((conn, _)) => TokioIo::new(conn), |
| 145 | + Err(e) => { |
| 146 | + log::warn!("failed to accept connection"; "error" => e); |
| 147 | + |
| 148 | + continue; |
| 149 | + } |
| 150 | + }; |
| 151 | + |
| 152 | + let router = this.router.clone(); |
| 153 | + |
| 154 | + tokio::spawn( |
| 155 | + hyper::server::conn::http1::Builder::new() |
| 156 | + // upgrades needed for websockets |
| 157 | + .serve_connection(socket, router) |
| 158 | + .with_upgrades(), |
| 159 | + ); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +fn bind_socket(addr: SocketAddr) -> BootstrapResult<Socket> { |
| 165 | + let socket = Socket::new( |
| 166 | + if addr.is_ipv4() { |
| 167 | + Domain::IPV4 |
| 168 | + } else { |
| 169 | + Domain::IPV6 |
| 170 | + }, |
| 171 | + Type::STREAM, |
| 172 | + None, |
| 173 | + )?; |
| 174 | + |
| 175 | + socket.set_reuse_address(true)?; |
| 176 | + #[cfg(unix)] |
| 177 | + socket.set_reuse_port(true)?; |
| 178 | + socket.bind(&SockAddr::from(addr))?; |
| 179 | + socket.listen(1024)?; |
| 180 | + |
| 181 | + Ok(socket) |
| 182 | +} |
| 183 | + |
| 184 | +#[cfg(all(target_os = "linux", feature = "memory-profiling"))] |
| 185 | +mod memory_profiling { |
| 186 | + use super::*; |
| 187 | + use crate::telemetry::MemoryProfiler; |
| 188 | + use crate::Result; |
| 189 | + |
| 190 | + pub(super) fn profiler(settings: Arc<TelemetrySettings>) -> Result<MemoryProfiler> { |
| 191 | + MemoryProfiler::get_or_init_with(&settings.memory_profiler)?.ok_or_else(|| { |
| 192 | + "profiling should be enabled via `_RJEM_MALLOC_CONF=prof:true` env var".into() |
| 193 | + }) |
| 194 | + } |
| 195 | + |
| 196 | + pub(super) async fn heap_profile(settings: Arc<TelemetrySettings>) -> Result<String> { |
| 197 | + profiler(settings)?.heap_profile().await |
| 198 | + } |
| 199 | + |
| 200 | + pub(super) async fn heap_stats(settings: Arc<TelemetrySettings>) -> Result<String> { |
| 201 | + profiler(settings)?.heap_stats() |
| 202 | + } |
| 203 | +} |
0 commit comments