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