tk_carbon/
lib.rs

1//! Carbon client protocol implementation
2//!
3//! [Documentation](https://docs.rs/tk-carbon) |
4//! [Github](https://github.com/tailhook/tk-carbon) |
5//! [Crate](https://crates.io/crates/tk-carbon)
6//!
7//! # High Level Interface
8//!
9//! ```rust,ignore
10//! let (carbon, init) = Carbon::new(&Config::new().done());
11//! init.connect_to(resolver.subscribe("localhost:2003"), &handle);
12//! // now you can submit metrics
13//! carbon.add_metric("my.metric", 10);
14//! ```
15//!
16//! This allows protocol to:
17//!
18//! 1. Establish connection to all addressses address resolves too
19//! 2. Reconnect in case of failure
20//! 3. Reconnect to new host(s) when IPs the DNS name resolves to changes
21//!
22//! See [examples](https://github.com/tailhook/tk-carbon/tree/master/examples)
23//! for more full examples.
24//!
25//! # Lower Level Interface
26//!
27//! In case you don't want to use connection pooling, you may connect carbon
28//! instance to a specific connection:
29//!
30//! ```rust,ignore
31//! use tk_carbon::{Carbon, Config};
32//!
33//! let (carbon, init) = Carbon::new(&Config::new().done());
34//! handle.spawn(TcpStream::connect(&addr, &handle)
35//!     .and_then(move |sock| init.from_connection(sock, &handle2))
36//!     .map_err(|e| unimplemented!()));
37//! // use carbon the same way as above
38//! carbon.add_metric("my.metric", 10);
39//! ```
40//!
41//! # General
42//!
43//! [`Carbon`](struct.Carbon.html) object is the same for connection pool and
44//! raw interface and may be used from any thread as long as the tokio loop
45//! running the networking code is still alive.
46//!
47//! You don't have to wait until connection is established to send metrics,
48//! they will be buffered up till configuration limit
49//! (see docs on [`Config`](struct.Config.html))
50//!
51#![warn(missing_docs)]
52
53extern crate abstract_ns;
54extern crate futures;
55extern crate num_traits;
56extern crate tokio_core;
57extern crate tokio_io;
58extern crate tk_bufstream;
59extern crate rand;
60extern crate void;
61
62#[macro_use] extern crate log;
63
64mod public;
65mod element;
66mod proto;
67mod pool;
68mod config;
69mod channel;
70
71pub use public::Carbon;
72pub use proto::Proto;
73
74use std::sync::Arc;
75use std::time::Duration;
76
77/// A helper structure for initializing protocol instance
78pub struct Init {
79    chan: channel::Receiver,
80    config: Arc<Config>,
81}
82
83/// Configuration of carbon protocol
84///
85/// This configuration is used both for single connection and connection pool.
86#[derive(Clone, Debug)]
87pub struct Config {
88    write_timeout: Duration,
89    watermarks: (usize, usize),
90    max_metrics_buffered: usize,
91
92    /// Reconnect delay in milliseconds, so it's easier to generate random
93    reconnect_delay: (u64, u64),
94}