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(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 the value of TCP_NODELAY option for accepted connections. Enabled by
54 /// default.
55 pub tcp_nodelay: Option<bool>,
56
57 /// Set whether HTTP2 Ping frames are enabled on accepted connections.
58 ///
59 /// If None is specified, HTTP2 keepalive is disabled, otherwise the
60 /// duration specified will be the time interval between HTTP2 Ping
61 /// frames. The timeout for receiving an acknowledgement
62 /// of the keepalive ping can be set with http2_keepalive_timeout.
63 ///
64 /// Default is no HTTP2 keepalive (None)
65 pub http2_keepalive_interval: Option<Duration>,
66
67 /// Sets a timeout for receiving an acknowledgement of the keepalive ping.
68 ///
69 /// If the ping is not acknowledged within the timeout, the connection will
70 /// be closed. Does nothing if http2_keep_alive_interval is disabled.
71 ///
72 /// Default is 20 seconds.
73 pub http2_keepalive_timeout: Option<Duration>,
74
75 // Only affects servers
76 pub load_shed: Option<bool>,
77
78 /// Only affects clients
79 pub rate_limit: Option<(u64, Duration)>,
80
81 // Only affects servers
82 pub global_concurrency_limit: Option<usize>,
83}
84
85impl Config {
86 pub fn new() -> Self {
87 Default::default()
88 }
89
90 pub fn server_builder(&self) -> ServerBuilder {
91 ServerBuilder::from_config(self, DefaultMetricsCallbackProvider::default())
92 }
93
94 pub fn server_builder_with_metrics<M>(&self, metrics_provider: M) -> ServerBuilder<M>
95 where
96 M: MetricsCallbackProvider,
97 {
98 ServerBuilder::from_config(self, metrics_provider)
99 }
100
101 pub async fn connect(
102 &self,
103 addr: &Multiaddr,
104 tls_config: Option<ClientConfig>,
105 ) -> Result<Channel> {
106 connect_with_config(addr, tls_config, self).await
107 }
108
109 pub fn connect_lazy(
110 &self,
111 addr: &Multiaddr,
112 tls_config: Option<ClientConfig>,
113 ) -> Result<Channel> {
114 connect_lazy_with_config(addr, tls_config, self)
115 }
116}