mysten_network/
config.rs

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