nsq_client/config.rs
1// MIT License
2//
3// Copyright (c) 2019-2021 Alessandro Cresto Miseroglio <alex179ohm@gmail.com>
4// Copyright (c) 2019-2021 Tangram Technologies S.R.L. <https://tngrm.io>
5//
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12//
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15//
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24use serde_derive::{Serialize, Deserialize};
25
26/// Configuration sent to nsqd to properly config the [Connection](struct.Connection.html)
27///
28/// # Examples
29///```no-run
30/// use nsq_client::{Connection, Config};
31///
32/// fn main() {
33/// let sys = System::new("consumer");
34/// let config = Config::new().client_id("consumer").user_agent("node-1");
35/// Supervisor::start(|_| Connection::new(
36/// "test",
37/// "test",
38/// "0.0.0.0:4150",
39/// Some(config),
40/// None,
41/// None,
42/// ));
43/// sys.run();
44/// }
45///```
46///
47#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
48pub struct Config {
49 /// Identifiers sent to nsqd representing this client (consumer specific)
50 ///
51 /// Default: **hostname** where connection is started
52 pub client_id: Option<String>,
53
54 /// Hostname where client is deployed.
55 ///
56 /// Default: **hostname** where connection is started
57 pub hostname: Option<String>,
58
59 /// Enable feature_negotiation
60 ///
61 /// Default: **true**
62 pub feature_negotiation: bool,
63
64 /// Duration of time between heartbeats (milliseconds).
65 ///
66 /// Valid values:
67 /// * -1 disables heartbeats
68 /// * 1000 <= heartbeat_interval <= configured_max
69 ///
70 /// Default: **30000**
71 pub heartbeat_interval: i64,
72
73 /// Size of the buffer (in bytes) used by nsqd for buffering writes to this connection
74 ///
75 /// Valid values:
76 /// * -1 disable output buffer
77 /// * 64 <= output_buffer_size <= configured_max
78 ///
79 /// Default: **16384**
80 pub output_buffer_size: u64,
81
82 /// The timeout after which data nsqd has buffered will be flushed to this client.
83 ///
84 /// Valid values:
85 /// * -1 disable buffer timeout
86 /// * 1ms <= output_buffer_timeout <= configured_max
87 ///
88 /// Default: **250**
89 pub output_buffer_timeout: u32,
90
91 /// Enable TLS negotiation
92 ///
93 /// Default: **false** (Not implemented)
94 pub tls_v1: bool,
95
96 /// Enable snappy compression.
97 ///
98 /// Default: **false** (Not implemented)
99 pub snappy: bool,
100
101 /// Enable deflate compression.
102 ///
103 /// Default: **false** (Not implemented)
104 pub deflate: bool,
105 /// Configure deflate compression level.
106 ///
107 /// Valid range:
108 /// * 1 <= deflate_level <= configured_max
109 ///
110 /// Default: **6**
111 pub deflate_level: u16,
112
113 /// Integer percentage to sample the channel.
114 ///
115 /// Deliver a perventage of all messages received to this connection.
116 ///
117 /// Default: **0**
118 pub sample_rate: u16,
119
120 /// String indentifying the agent for this connection.
121 ///
122 /// Default: **hostname** where connection is started
123 pub user_agent: String,
124
125 /// Timeout used by nsqd before flushing buffered writes (set to 0 to disable).
126 ///
127 /// Default: **0**
128 pub message_timeout: u32,
129
130}
131use hostname::get_hostname;
132
133impl Default for Config {
134 fn default() -> Config {
135 Config {
136 client_id: get_hostname(),
137 user_agent: String::from("nsq_client"),
138 hostname: get_hostname(),
139 deflate: false,
140 deflate_level: 6,
141 snappy: false,
142 feature_negotiation: true,
143 heartbeat_interval: 30000,
144 message_timeout: 0,
145 output_buffer_size: 16384,
146 output_buffer_timeout: 250,
147 sample_rate: 0,
148 tls_v1: false,
149 }
150 }
151}
152
153#[derive(Clone, Debug, Deserialize)]
154pub struct NsqdConfig {
155 pub max_rdy_count: u32,
156 pub version: String,
157 pub max_msg_timeout: u64,
158 pub msg_timeout: u64,
159 pub tls_v1: bool,
160 pub deflate: bool,
161 pub deflate_level: u16,
162 pub max_deflate_level: u16,
163 pub snappy: bool,
164 pub sample_rate: u16,
165 pub auth_required: bool,
166 pub output_buffer_size: u64,
167 pub output_buffer_timeout: u32,
168}
169
170#[allow(dead_code)]
171impl Config {
172 /// Create default [Config](struct.Config.html)
173 /// ```no-run
174 /// use nsq_client::{Config};
175 ///
176 /// fn main() {
177 /// let config = Config::new();
178 /// assert_eq!(config, Config::default());
179 /// }
180 /// ```
181 pub fn new() -> Config {
182 Config{ ..Default::default() }
183 }
184
185 /// Change [client_id](struct.Config.html#structfield.client_id)
186 /// ```no-run
187 /// use nsq_client::Config;
188 ///
189 /// fn main() {
190 /// let config = Config::new().client_id("consumer");
191 /// assert_eq!(config.client_id, Some("consumer".to_owned()));
192 /// }
193 /// ```
194 pub fn client_id<S: Into<String>>(mut self, client_id: S) -> Self {
195 self.client_id = Some(client_id.into());
196 self
197 }
198
199 /// Change [hostname](struct.Config.html#structfield.hostname)
200 /// ```no-run
201 /// use nsq_client::Config;
202 ///
203 /// fn main() {
204 /// let config = Config::new().hostname("node-1");
205 /// assert_eq!(config.hostname, Some("node-1".to_owned()));
206 /// }
207 /// ```
208 pub fn hostname<S: Into<String>>(mut self, hostname: S) -> Self {
209 self.hostname = Some(hostname.into());
210 self
211 }
212
213 /// Change [user_agent](struct.Config.html#structfield.user_agent)
214 /// ```no-run
215 /// use nsq_client::Config;
216 ///
217 /// fn main() {
218 /// let config = Config::new().user_agent("consumer-1");
219 /// assert_eq!(config.user_agent, Some("consumer-1".to_owned()));
220 /// }
221 /// ```
222 pub fn user_agent<S: Into<String>>(mut self, user_agent: S) -> Self {
223 self.user_agent = user_agent.into();
224 self
225 }
226}