tk_carbon/
config.rs

1use std::sync::Arc;
2use std::time::Duration;
3
4use {Config};
5
6pub fn to_ms(dur: Duration) -> u64 {
7    return dur.as_secs() + (dur.subsec_nanos() / 1000000) as u64;
8}
9
10impl Config {
11    /// Create the config builder with all defaults
12    pub fn new() -> Config {
13        Config {
14            write_timeout: Duration::new(10, 0),
15            watermarks: (60_000, 1_048_576),
16            max_metrics_buffered: 10000,
17
18            reconnect_delay: (50, 150),
19        }
20    }
21
22    /// Set the reconnect delay
23    ///
24    /// Actual delay is chosen each time as a random millisecond
25    /// from 0.5 of this value to 1.5 for this value.
26    ///
27    /// Note: reconnect delay is calculated since previous connection attempt.
28    /// I.e. if connection is broken after a minute of normal work it will
29    /// reconnect immediately.
30    pub fn reconnect_delay(&mut self, delay: Duration) -> &mut Self {
31        let ms = to_ms(delay);
32        self.reconnect_delay = (ms/2, ms*3/2);
33        self
34    }
35
36    /// Set the reconnect delay bounds to more specific values
37    ///
38    /// Actual delay is chosen each time as a random millisecond
39    /// from the range of `min_delay..max_delay`. Usually it's enough to set
40    /// `reconnect_delay` but you can use this method if you need more tight
41    /// control.
42    ///
43    /// Note: reconnect delay is calculated since previous connection attempt.
44    /// I.e. if connection is broken after a minute of normal work it will
45    /// reconnect immediately.
46    pub fn reconnect_delay_min_max(&mut self,
47        min_delay: Duration, max_delay: Duration)
48        -> &mut Self
49    {
50        self.reconnect_delay = (to_ms(min_delay), to_ms(max_delay));
51        self
52    }
53
54    /// Timeout of writing at least some byte when there are any bytes in the
55    /// outgoing buffer
56    pub fn write_timeout(&mut self, dur: Duration) -> &mut Self {
57        self.write_timeout = dur;
58        self
59    }
60
61    /// Buffer limits or watermarks
62    ///
63    /// The rules of thumb to not to loose any metrics:
64    ///
65    /// * Low watermark is efficient buffer size for sending to network
66    /// * High watermark is maximum buffer size, should be much larger to
67    ///   compensate when individual host becomes slower
68    ///
69    /// # Details
70    ///
71    /// When number of bytes buffered reaches low watermark (in all
72    /// connections) we stop pulling metrics from the internal channel.
73    /// The latter will start to drop messages after `max_metrics_buffered`
74    /// is reached.
75    ///
76    /// When number of bytes buffered reaches high watermark in any specific
77    /// connection we drop connection entirely, as this means connection can't
78    /// keep up with the traffic (but `write_timeout` has not reached for
79    /// some reason, for example it accepts bytes in small chunks).
80    ///
81    /// High water mark should be at least one metric larger than low watermark
82    /// or connection will be dropped whenever watermark is reached. For
83    /// single connection high watermark should still be higher, but otherwise
84    /// there is no chance it will be reached beyond single metric.
85    ///
86    /// # Panics
87    ///
88    /// Panics if high watermark is smaller than low watermark or watermark
89    /// is zero.
90    pub fn watermarks(&mut self, low: usize, high: usize) -> &mut Self {
91        assert!(low > 0);
92        assert!(high >= low);
93        self.watermarks = (low, high);
94        self
95    }
96
97    /// Maximum metrics buffered in a channel
98    ///
99    /// The rule of thumb: this channel should contain as much metrics as might
100    /// be sent within the period of the downtime + reconnect time for the
101    /// carbon backend (in order to not to loose any values). Probably 10
102    /// seconds or 30 seconds worth of metrics at least.
103    ///
104    /// This buffer is common for all the underlying channel between `Carbon`
105    /// instance and the actual `Proto` or `Pool`. This channel is single
106    /// one for all underlying connections.
107    pub fn max_metrics_buffered(&mut self, metrics: usize) -> &mut Self {
108        self.max_metrics_buffered = metrics;
109        self
110    }
111
112    /// Create a Arc'd config clone to pass to the constructor
113    ///
114    /// This is just a convenience method.
115    pub fn done(&mut self) -> Arc<Config> {
116        Arc::new(self.clone())
117    }
118}