mysten_network/
config.rs

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