litep2p/transport/tcp/
config.rs

1// Copyright 2023 litep2p developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! TCP transport configuration.
22
23use crate::{
24    crypto::noise::{MAX_READ_AHEAD_FACTOR, MAX_WRITE_BUFFER_SIZE},
25    transport::{CONNECTION_OPEN_TIMEOUT, SUBSTREAM_OPEN_TIMEOUT},
26};
27
28/// TCP transport configuration.
29#[derive(Debug, Clone)]
30pub struct Config {
31    /// Listen address for the transport.
32    ///
33    /// Default listen addresses are ["/ip4/0.0.0.0/tcp/0", "/ip6/::/tcp/0"].
34    pub listen_addresses: Vec<multiaddr::Multiaddr>,
35
36    /// Whether to set `SO_REUSEPORT` and bind a socket to the listen address port for outbound
37    /// connections.
38    ///
39    /// Note that `SO_REUSEADDR` is always set on listening sockets.
40    ///
41    /// Defaults to `true`.
42    pub reuse_port: bool,
43
44    /// Enable `TCP_NODELAY`.
45    ///
46    /// Defaults to `false`.
47    pub nodelay: bool,
48
49    /// Yamux configuration.
50    pub yamux_config: crate::yamux::Config,
51
52    /// Noise read-ahead frame count.
53    ///
54    /// Specifies how many Noise frames are read per call to the underlying socket.
55    ///
56    /// By default this is configured to `5` so each call to the underlying socket can read up
57    /// to `5` Noise frame per call. Fewer frames may be read if there isn't enough data in the
58    /// socket. Each Noise frame is `65 KB` so the default setting allocates `65 KB * 5 = 325 KB`
59    /// per connection.
60    pub noise_read_ahead_frame_count: usize,
61
62    /// Noise write buffer size.
63    ///
64    /// Specifes how many Noise frames are tried to be coalesced into a single system call.
65    /// By default the value is set to `2` which means that the `NoiseSocket` will allocate
66    /// `130 KB` for each outgoing connection.
67    ///
68    /// The write buffer size is separate from the read-ahead frame count so by default
69    /// the Noise code will allocate `2 * 65 KB + 5 * 65 KB = 455 KB` per connection.
70    pub noise_write_buffer_size: usize,
71
72    /// Connection open timeout.
73    ///
74    /// How long should litep2p wait for a connection to be opened before the host
75    /// is deemed unreachable.
76    pub connection_open_timeout: std::time::Duration,
77
78    /// Substream open timeout.
79    ///
80    /// How long should litep2p wait for a substream to be opened before considering
81    /// the substream rejected.
82    pub substream_open_timeout: std::time::Duration,
83}
84
85impl Default for Config {
86    fn default() -> Self {
87        Self {
88            listen_addresses: vec![
89                "/ip4/0.0.0.0/tcp/0".parse().expect("valid address"),
90                "/ip6/::/tcp/0".parse().expect("valid address"),
91            ],
92            reuse_port: true,
93            nodelay: false,
94            yamux_config: Default::default(),
95            noise_read_ahead_frame_count: MAX_READ_AHEAD_FACTOR,
96            noise_write_buffer_size: MAX_WRITE_BUFFER_SIZE,
97            connection_open_timeout: CONNECTION_OPEN_TIMEOUT,
98            substream_open_timeout: SUBSTREAM_OPEN_TIMEOUT,
99        }
100    }
101}