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
use crate::connect; use crate::error::Error; use crate::rmps::encode as rencode; use crate::worker::{Message, Worker}; use crossbeam_channel::{unbounded, Sender}; use serde::Serialize; use std::fmt::Debug; use std::io; use std::net::ToSocketAddrs; use std::sync::atomic::{AtomicBool, Ordering}; use std::time::{Duration, SystemTime}; pub trait Client { fn send<A>(&self, tag: String, a: &A, timestamp: SystemTime) -> Result<(), Error> where A: Serialize; fn send_flush(&self) -> Result<(), Error>; fn close(&mut self); } pub struct WorkerPool { worker: Worker, sender: Sender<Message>, closed: AtomicBool, } impl WorkerPool { pub fn create<A>(addr: &A) -> io::Result<WorkerPool> where A: ToSocketAddrs + Clone + Debug + Send + 'static, { WorkerPool::with_settings(addr, &Default::default()) } pub fn with_settings<A>(addr: &A, settings: &Settings) -> io::Result<WorkerPool> where A: ToSocketAddrs + Clone + Debug + Send + 'static, { let (sender, receiver) = unbounded(); info!("Worker creating..."); let conn_settings = connect::ConnectionSettings { connect_retry_initial_delay: settings.connection_retry_initial_delay, connect_retry_max_delay: settings.connection_retry_max_delay, connect_retry_timeout: settings.connection_retry_timeout, write_timeout: settings.write_timeout, read_timeout: settings.read_timeout, write_retry_initial_delay: settings.write_retry_initial_delay, write_retry_max_delay: settings.write_retry_max_delay, write_retry_timeout: settings.write_retry_timeout, read_retry_initial_delay: settings.read_retry_initial_delay, read_retry_max_delay: settings.read_retry_max_delay, read_retry_timeout: settings.read_retry_timeout, }; let worker = Worker::create( addr.clone(), conn_settings, receiver, settings.flush_period, settings.max_flush_entries, )?; Ok(WorkerPool { worker, sender, closed: AtomicBool::new(false), }) } } impl Client for WorkerPool { fn send<A>(&self, tag: String, a: &A, timestamp: SystemTime) -> Result<(), Error> where A: Serialize, { if self.closed.load(Ordering::Acquire) { debug!("Worker does already closed."); return Ok(()); } let mut buf = Vec::new(); rencode::write_named(&mut buf, a).map_err(|e| Error::DeriveError(e.to_string()))?; self.sender .send(Message::Queuing(tag, timestamp, buf)) .map_err(|e| Error::SendError(e.to_string()))?; Ok(()) } fn send_flush(&self) -> Result<(), Error> { self.sender .send(Message::Flushing) .map_err(|e| Error::SendError(e.to_string()))?; Ok(()) } fn close(&mut self) { if self.closed.fetch_or(true, Ordering::SeqCst) { info!("Worker does already closed."); return; } info!("Sending terminate message to worker."); self.sender.send(Message::Terminate).unwrap(); info!("Shutting down worker."); let wkr = &mut self.worker; wkr.join_handler(); } } impl Drop for WorkerPool { fn drop(&mut self) { self.close() } } #[derive(Clone)] pub struct Settings { pub flush_period: Duration, pub max_flush_entries: usize, pub connection_retry_initial_delay: Duration, pub connection_retry_max_delay: Duration, pub connection_retry_timeout: Duration, pub write_timeout: Duration, pub read_timeout: Duration, pub write_retry_initial_delay: Duration, pub write_retry_max_delay: Duration, pub write_retry_timeout: Duration, pub read_retry_initial_delay: Duration, pub read_retry_max_delay: Duration, pub read_retry_timeout: Duration, } impl Default for Settings { fn default() -> Self { Settings { flush_period: Duration::from_millis(256), max_flush_entries: 1024, connection_retry_initial_delay: Duration::from_millis(50), connection_retry_max_delay: Duration::from_secs(5), connection_retry_timeout: Duration::from_secs(60), write_retry_initial_delay: Duration::from_millis(5), write_retry_max_delay: Duration::from_secs(5), write_retry_timeout: Duration::from_secs(30), read_retry_initial_delay: Duration::from_millis(5), read_retry_max_delay: Duration::from_secs(5), read_retry_timeout: Duration::from_secs(10), write_timeout: Duration::from_secs(1), read_timeout: Duration::from_secs(1), } } } #[cfg(test)] mod test {}