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
//! [r2d2](https://github.com/sfackler/r2d2) connection pool protocol for InfluxDB.

pub extern crate influxdb;
pub extern crate r2d2;

pub mod ext;

use ext::SyncClient;
use influxdb::{Client, Error as InfluxDBError};

use std::convert::From;
use std::error;
use std::error::Error as _StdError;
use std::fmt;

/// An extension for influxdb::Error for the Error trait
#[derive(Debug)]
pub enum Error {
    Other(InfluxDBError),
}

impl fmt::Display for Error {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(fmt, "{}", self.description())
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        match *self {
            Error::Other(ref err) => match err {
                InfluxDBError::InvalidQueryError { .. } => "query is invalid",
                InfluxDBError::UrlConstructionError { .. } => "failed to build URL",
                InfluxDBError::ProtocolError { .. } => "http protocol error",
                InfluxDBError::DeserializationError { .. } => "http protocol error",
                InfluxDBError::DatabaseError { .. } => "database error",
                InfluxDBError::AuthenticationError => "authentication error",
                InfluxDBError::AuthorizationError => "authorization error",
                InfluxDBError::ConnectionError { .. } => "connection error",
            },
        }
    }
}

#[derive(Clone, Debug)]
pub struct InfluxDBConnectionInfo {
    url: String,
    database: String,
    username: Option<String>,
    password: Option<String>,
    is_auth: bool,
}

#[derive(Debug)]
pub struct InfluxDBConnectionManager {
    info: InfluxDBConnectionInfo,
}

impl InfluxDBConnectionManager {
    pub fn new<T: Into<InfluxDBConnectionInfo>>(params: T) -> InfluxDBConnectionManager {
        InfluxDBConnectionManager {
            info: params.into(),
        }
    }
}

/// InfluxDB connection info
pub struct SimpleInfo {
    pub url: String,
    pub database: String,
}

/// InfluxDB connection info with Authenticator
pub struct AuthInfo {
    pub url: String,
    pub database: String,
    pub username: String,
    pub password: String,
}

impl From<SimpleInfo> for InfluxDBConnectionInfo {
    fn from(item: SimpleInfo) -> Self {
        InfluxDBConnectionInfo {
            url: item.url,
            database: item.database,
            username: None,
            password: None,
            is_auth: false,
        }
    }
}

impl From<AuthInfo> for InfluxDBConnectionInfo {
    fn from(item: AuthInfo) -> Self {
        InfluxDBConnectionInfo {
            url: item.url,
            database: item.database,
            username: Some(item.username),
            password: Some(item.password),
            is_auth: true,
        }
    }
}

/// An `r2d2::ConnectionManager` for `InfluxDB::Client`s.
///
/// ## Example
///

/// ```rust
/// extern crate r2d2_influxdb;
///
/// use r2d2_influxdb::{r2d2, InfluxDBConnectionManager, SimpleInfo};
/// use std::thread;
///
/// fn main() {
///     let info = SimpleInfo {
///         url: "http://localhost:8086".into(),
///         database: "test".into(),
///     };
///     let manager = InfluxDBConnectionManager::new(info);
///     let pool = r2d2::Pool::builder().build(manager).unwrap();
///
///     let mut handles = vec![];
///
///     for _i in 0..10i32 {
///         let pool = pool.clone();
///         handles.push(thread::spawn(move || {
///             let conn = pool.get().unwrap();
///             let reply = conn.ping();
///             println!("{:?}", reply);
///             assert!(reply.is_ok())
///         }));
///     }
///
///     for h in handles {
///         h.join().unwrap();
///     }
/// }
/// ```
impl r2d2::ManageConnection for InfluxDBConnectionManager {
    type Connection = SyncClient;
    type Error = Error;

    fn connect(&self) -> Result<SyncClient, Error> {
        let client = if self.info.is_auth {
            Client::new(self.info.url.to_owned(), self.info.database.to_owned()).with_auth(
                self.info.username.to_owned().unwrap(),
                self.info.password.to_owned().unwrap(),
            )
        } else {
            Client::new(self.info.url.to_owned(), self.info.database.to_owned())
        };
        Ok(SyncClient::new(client))
    }

    fn is_valid(&self, conn: &mut SyncClient) -> Result<(), Error> {
        conn.ping().map_err(Error::Other).map(|_| ())
    }

    fn has_broken(&self, conn: &mut SyncClient) -> bool {
        self.is_valid(conn).is_err()
    }
}