tcp_client/
config.rs

1//! Global configuration for this [`crate`].
2//!
3//! You may change the configuration by calling [`set_config`] function.
4//!
5//! # Example
6//! ```rust
7//! use tcp_client::config::{ClientConfig, set_config};
8//!
9//! # fn main() {
10//! set_config(ClientConfig::default());
11//! # }
12//! ```
13
14use std::sync::RwLock;
15use std::time::Duration;
16use once_cell::sync::Lazy;
17
18/// Global configuration.
19///
20/// # Example
21/// ```rust
22/// use tcp_client::config::ClientConfig;
23///
24/// # fn main() {
25/// let config = ClientConfig::default();
26/// # let _ = config;
27/// # }
28/// ```
29#[derive(Debug, Copy, Clone, Eq, PartialEq)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(default))]
31pub struct ClientConfig {
32    /// `connect_timeout` is the timeout of connecting to the server.
33    ///
34    /// The default value is `10s`.
35    ///
36    /// # Example
37    /// ```rust
38    /// use tcp_client::config::ClientConfig;
39    ///
40    /// # fn main() {
41    /// use std::time::Duration;
42    /// let config = ClientConfig {
43    ///     connect_timeout: Duration::from_secs(10),
44    ///     ..ClientConfig::default()
45    /// };
46    /// # let _ = config;
47    /// # }
48    /// ```
49    pub connect_timeout: Duration,
50
51    /// `receive_timeout` is the timeout of receiving from the server.
52    ///
53    /// The default value is `30s`.
54    ///
55    /// # Example
56    /// ```rust
57    /// use tcp_client::config::ClientConfig;
58    ///
59    /// # fn main() {
60    /// use std::time::Duration;
61    /// let config = ClientConfig {
62    ///     receive_timeout: Duration::from_secs(30),
63    ///     ..ClientConfig::default()
64    /// };
65    /// # let _ = config;
66    /// # }
67    /// ```
68    pub receive_timeout: Duration,
69}
70
71impl Default for ClientConfig {
72    fn default() -> Self {
73        Self {
74            connect_timeout: Duration::from_secs(10),
75            receive_timeout: Duration::from_secs(30),
76        }
77    }
78}
79
80static CONFIG: Lazy<RwLock<ClientConfig>> = Lazy::new(|| RwLock::new(ClientConfig::default()));
81
82/// Set the global configuration.
83///
84/// This function is recommended to only be called once during initialization.
85///
86/// # Example
87/// ```rust
88/// use tcp_client::config::{ClientConfig, set_config};
89///
90/// # fn main() {
91/// set_config(ClientConfig::default());
92/// # }
93/// ```
94#[inline]
95pub fn set_config(config: ClientConfig) {
96    let mut c = CONFIG.write().unwrap();
97    *c = config;
98}
99
100/// Get the global configuration.
101///
102/// # Example
103/// ```rust
104/// use tcp_client::config::get_config;
105///
106/// # fn main() {
107/// let config = get_config();
108/// # let _ = config;
109/// # }
110/// ```
111#[inline]
112pub fn get_config() -> ClientConfig {
113    let c = CONFIG.read().unwrap();
114    (*c).clone()
115}
116
117/// A cheaper shortcut of
118/// ```rust,ignore
119/// get_config().connect_timeout
120/// ```
121#[inline]
122pub fn get_connect_timeout() -> Duration {
123    let c = CONFIG.read().unwrap();
124    (*c).connect_timeout
125}
126
127/// A cheaper shortcut of
128/// ```rust,ignore
129/// get_config().idle_sec
130/// ```
131#[inline]
132pub fn get_receive_timeout() -> Duration {
133    let c = CONFIG.read().unwrap();
134    (*c).receive_timeout
135}