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