Skip to main content

iota_network_stack/
config.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::time::Duration;
6
7use eyre::Result;
8use serde::{Deserialize, Serialize};
9use tokio_rustls::rustls::ClientConfig;
10use tonic::transport::Channel;
11
12use crate::{
13    Multiaddr,
14    client::{connect_lazy_with_config, connect_with_config},
15    metrics::{DefaultMetricsCallbackProvider, MetricsCallbackProvider},
16    server::ServerBuilder,
17};
18
19#[derive(Clone, Debug, Default, Deserialize, Serialize)]
20pub struct Config {
21    /// Set the concurrency limit applied to on requests inbound per connection.
22    pub concurrency_limit_per_connection: Option<usize>,
23
24    /// Set a timeout for all request handlers.
25    pub request_timeout: Option<Duration>,
26
27    /// Set a timeout for establishing an outbound connection.
28    pub connect_timeout: Option<Duration>,
29
30    /// Sets the SETTINGS_INITIAL_WINDOW_SIZE option for HTTP2 stream-level flow
31    /// control. Default is 65,535
32    pub http2_initial_stream_window_size: Option<u32>,
33
34    /// Sets the max connection-level flow control for HTTP2
35    ///
36    /// Default is 65,535
37    pub http2_initial_connection_window_size: Option<u32>,
38
39    /// Sets the SETTINGS_MAX_CONCURRENT_STREAMS option for HTTP2 connections.
40    ///
41    /// Default is no limit (None).
42    pub http2_max_concurrent_streams: Option<u32>,
43
44    /// Set whether TCP keepalive messages are enabled on accepted connections.
45    ///
46    /// If None is specified, keepalive is disabled, otherwise the duration
47    /// specified will be the time to remain idle before sending TCP
48    /// keepalive probes.
49    ///
50    /// Default is no keepalive (None)
51    pub tcp_keepalive: Option<Duration>,
52
53    /// Set whether HTTP2 Ping frames are enabled on accepted connections.
54    ///
55    /// If None is specified, HTTP2 keepalive is disabled, otherwise the
56    /// duration specified will be the time interval between HTTP2 Ping
57    /// frames. The timeout for receiving an acknowledgement
58    /// of the keepalive ping can be set with http2_keepalive_timeout.
59    ///
60    /// Default is no HTTP2 keepalive (None)
61    pub http2_keepalive_interval: Option<Duration>,
62
63    /// Sets a timeout for receiving an acknowledgement of the keepalive ping.
64    ///
65    /// If the ping is not acknowledged within the timeout, the connection will
66    /// be closed. Does nothing if http2_keep_alive_interval is disabled.
67    ///
68    /// Default is 20 seconds.
69    pub http2_keepalive_timeout: Option<Duration>,
70
71    // Only affects servers
72    pub load_shed: Option<bool>,
73
74    /// Only affects clients
75    pub rate_limit: Option<(u64, Duration)>,
76
77    // Only affects servers
78    pub global_concurrency_limit: Option<usize>,
79}
80
81impl Config {
82    pub fn new() -> Self {
83        Default::default()
84    }
85
86    pub fn server_builder(&self) -> ServerBuilder {
87        ServerBuilder::from_config(self, DefaultMetricsCallbackProvider::default())
88    }
89
90    pub fn server_builder_with_metrics<M>(&self, metrics_provider: M) -> ServerBuilder<M>
91    where
92        M: MetricsCallbackProvider,
93    {
94        ServerBuilder::from_config(self, metrics_provider)
95    }
96
97    pub async fn connect(&self, addr: &Multiaddr, tls_config: ClientConfig) -> Result<Channel> {
98        connect_with_config(addr, tls_config, self).await
99    }
100
101    pub fn connect_lazy(&self, addr: &Multiaddr, tls_config: ClientConfig) -> Result<Channel> {
102        connect_lazy_with_config(addr, tls_config, self)
103    }
104
105    pub(crate) fn http_config(&self) -> iota_http::Config {
106        iota_http::Config::default()
107            .initial_stream_window_size(self.http2_initial_stream_window_size)
108            .initial_connection_window_size(self.http2_initial_connection_window_size)
109            .max_concurrent_streams(self.http2_max_concurrent_streams)
110            .http2_keepalive_timeout(self.http2_keepalive_timeout)
111            .http2_keepalive_interval(self.http2_keepalive_interval)
112            .tcp_keepalive(self.tcp_keepalive)
113    }
114}