Skip to main content

miden_node_utils/grpc/
server.rs

1use std::time::Duration;
2
3use tokio::net::TcpListener;
4use tonic::transport::Server;
5use tonic::transport::server::TcpIncoming;
6
7const HTTP2_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(60);
8const HTTP2_KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(20);
9const TCP_KEEPALIVE_IDLE: Duration = Duration::from_secs(60);
10const TCP_KEEPALIVE_INTERVAL: Duration = Duration::from_secs(10);
11const TCP_KEEPALIVE_RETRIES: u32 = 3;
12
13/// Creates a gRPC server builder with HTTP/2 liveness checks.
14///
15/// A peer must acknowledge keepalive PINGs even when no application data is available.
16pub fn server_builder() -> Server {
17    Server::builder()
18        .http2_keepalive_interval(Some(HTTP2_KEEPALIVE_INTERVAL))
19        .http2_keepalive_timeout(Some(HTTP2_KEEPALIVE_TIMEOUT))
20}
21
22/// Creates an incoming stream with TCP liveness checks on accepted sockets.
23///
24/// TCP probes cover HTTP/1 connections as well as HTTP/2 connections. Tonic ignores its server TCP
25/// settings when the caller supplies an incoming stream. Some platforms use their system defaults
26/// for the probe interval or retry count.
27pub fn tcp_incoming(listener: TcpListener) -> TcpIncoming {
28    TcpIncoming::from(listener)
29        .with_keepalive(Some(TCP_KEEPALIVE_IDLE))
30        .with_keepalive_interval(Some(TCP_KEEPALIVE_INTERVAL))
31        .with_keepalive_retries(Some(TCP_KEEPALIVE_RETRIES))
32}