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::{num::NonZeroU32, 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/// Settings for clients, and for the gRPC layers of a server. A server's
20/// connection and HTTP/2 settings are an [`iota_http::Config`], passed to
21/// [`ServerBuilder::http_config`].
22#[derive(Clone, Debug, Default, Deserialize, Serialize)]
23pub struct Config {
24 /// Only affects clients. How many requests a connection may have in
25 /// flight at once.
26 pub concurrency_limit_per_connection: Option<usize>,
27
28 /// Set a timeout for all request handlers.
29 pub request_timeout: Option<Duration>,
30
31 /// Only affects clients. Set a timeout for establishing an outbound
32 /// connection.
33 pub connect_timeout: Option<Duration>,
34
35 /// Only affects clients. Sets the SETTINGS_INITIAL_WINDOW_SIZE option for
36 /// HTTP2 stream-level flow control.
37 ///
38 /// Default is 65,535
39 pub http2_initial_stream_window_size: Option<NonZeroU32>,
40
41 /// Only affects clients. Sets the max connection-level flow control for
42 /// HTTP2
43 ///
44 /// Default is 65,535
45 pub http2_initial_connection_window_size: Option<NonZeroU32>,
46
47 /// Only affects clients. Set whether TCP keepalive messages are enabled on
48 /// outbound connections.
49 ///
50 /// If None is specified, keepalive is disabled, otherwise the duration
51 /// specified will be the time to remain idle before sending TCP
52 /// keepalive probes.
53 ///
54 /// Default is no keepalive (None)
55 pub tcp_keepalive: Option<Duration>,
56
57 /// Only affects clients. Set whether HTTP2 Ping frames are enabled on
58 /// outbound connections.
59 ///
60 /// If None is specified, HTTP2 keepalive is disabled, otherwise the
61 /// duration specified will be the time interval between HTTP2 Ping
62 /// frames. The timeout for receiving an acknowledgement
63 /// of the keepalive ping can be set with http2_keepalive_timeout.
64 ///
65 /// Default is no HTTP2 keepalive (None)
66 pub http2_keepalive_interval: Option<Duration>,
67
68 /// Only affects clients. Sets a timeout for receiving an acknowledgement
69 /// of the keepalive ping.
70 ///
71 /// If the ping is not acknowledged within the timeout, the connection will
72 /// be closed. Does nothing if http2_keep_alive_interval is disabled.
73 ///
74 /// Default is 20 seconds.
75 pub http2_keepalive_timeout: Option<Duration>,
76
77 // Only affects servers
78 pub load_shed: Option<bool>,
79
80 /// Only affects clients
81 pub rate_limit: Option<(u64, Duration)>,
82
83 // Only affects servers
84 pub global_concurrency_limit: Option<usize>,
85}
86
87impl Config {
88 pub fn new() -> Self {
89 Default::default()
90 }
91
92 pub fn server_builder(&self) -> ServerBuilder {
93 ServerBuilder::from_config(self, DefaultMetricsCallbackProvider::default())
94 }
95
96 pub fn server_builder_with_metrics<M>(&self, metrics_provider: M) -> ServerBuilder<M>
97 where
98 M: MetricsCallbackProvider,
99 {
100 ServerBuilder::from_config(self, metrics_provider)
101 }
102
103 pub async fn connect(&self, addr: &Multiaddr, tls_config: ClientConfig) -> Result<Channel> {
104 connect_with_config(addr, tls_config, self).await
105 }
106
107 pub fn connect_lazy(&self, addr: &Multiaddr, tls_config: ClientConfig) -> Result<Channel> {
108 connect_lazy_with_config(addr, tls_config, self)
109 }
110}