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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Provides the connection manager the integrates bolt_client with mobc.

use async_trait::async_trait;
use bolt_client::{Client, Metadata, Stream};
use bolt_proto::message::Success;
use bolt_proto::version::{V1_0, V2_0, V3_0, V4_0, V4_1};
use bolt_proto::{Message, Value};
use mobc::Manager;
use std::collections::HashMap;
use std::convert::TryFrom;
use std::iter::FromIterator;
use std::net::SocketAddr;
use tokio::io::BufStream;
use tokio::net::lookup_host;
use tokio::net::ToSocketAddrs;
use tokio_util::compat::*;

pub use error::Error;

mod error;

/// A Bolt connection manager, used by mobc to create and test the health of database connections.
///
/// # Examples
///
/// ```rust,no_run
/// # use bolt_proto::version::V4_1;
/// # use mobc::{Manager, Pool};
/// # use mobc_boltrs::BoltConnectionManager;
/// # use std::collections::HashMap;
/// # use std::iter::FromIterator;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
///     let manager = BoltConnectionManager::new(
///         "localhost:7687",
///         None,
///         [V4_1, 0, 0, 0],
///         HashMap::from_iter(vec![
///             ("user_agent", "bolt-client/X.Y.Z"),
///             ("scheme", "basic"),
///             ("principal", "username"),
///             ("credentials", "password"),
///         ]),
///     )
///     .await?;
///
///     let pool = Pool::builder().max_open(20).build(manager);
///     let client = pool.get().await?;
///
/// #   Ok(())
/// # }
/// ```
pub struct BoltConnectionManager {
    addr: SocketAddr,
    domain: Option<String>,
    preferred_versions: [u32; 4],
    metadata: HashMap<String, Value>,
}

impl BoltConnectionManager {
    /// Creates a new [`BoltConnectionManager`]. Required arguments are the address and, if
    /// applicable the domain, of the database, preferred versions, and a hash map of metadata,
    /// such as authentication credentials.
    ///
    /// [`BoltConnectionManager`]: ./struct.BoltConnectionManager.html
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # use bolt_proto::version::V4_1;
    /// # use mobc::{Manager, Pool};
    /// # use mobc_boltrs::BoltConnectionManager;
    /// # use std::collections::HashMap;
    /// # use std::iter::FromIterator;
    ///
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let manager = BoltConnectionManager::new(
    ///         "localhost:7687",
    ///         None,
    ///         [V4_1, 0, 0, 0],
    ///         HashMap::from_iter(vec![
    ///             ("user_agent", "bolt-client/X.Y.Z"),
    ///             ("scheme", "basic"),
    ///             ("principal", "username"),
    ///             ("credentials", "password"),
    ///         ]),
    ///     )
    ///     .await?;
    ///
    ///     let pool = Pool::builder().max_open(20).build(manager);
    ///     let client = pool.get().await?;
    ///
    /// #   Ok(())
    /// # }
    /// ```
    pub async fn new(
        addr: impl ToSocketAddrs,
        domain: Option<String>,
        preferred_versions: [u32; 4],
        metadata: HashMap<impl Into<String>, impl Into<Value>>,
    ) -> Result<Self, Error> {
        Ok(Self {
            addr: lookup_host(addr)
                .await?
                .next()
                .ok_or(Error::InvalidAddress)?,
            domain,
            preferred_versions,
            metadata: metadata
                .into_iter()
                .map(|(k, v)| (k.into(), v.into()))
                .collect(),
        })
    }
}

#[async_trait]
impl Manager for BoltConnectionManager {
    type Connection = Client<Compat<BufStream<Stream>>>;
    type Error = Error;

    async fn connect(&self) -> Result<Self::Connection, Self::Error> {
        let mut client = Client::new(
            BufStream::new(Stream::connect(self.addr, self.domain.as_ref()).await?).compat(),
            &self.preferred_versions,
        )
        .await?;
        let response = match client.version() {
            V1_0 | V2_0 => {
                let mut metadata = self.metadata.clone();
                let user_agent: String = metadata
                    .remove("user_agent")
                    .ok_or_else(|| Error::InvalidMetadata {
                        metadata: "must contain a user_agent".to_string(),
                    })
                    .map(String::try_from)??;
                client.init(user_agent, Metadata::from(metadata)).await?
            }
            V3_0 | V4_0 | V4_1 => {
                client
                    .hello(Some(Metadata::from(self.metadata.clone())))
                    .await?
            }
            _ => {
                return Err(Error::InvalidClientVersion {
                    version: client.version(),
                })
            }
        };

        match response {
            Message::Success(_) => Ok(client),
            other => Err(Error::ClientInitFailed { message: other }),
        }
    }

    async fn check(&self, mut conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
        let response = match conn.version() {
            V1_0 | V2_0 => conn.run("RETURN 1;".to_string(), None).await?,
            V3_0 | V4_0 | V4_1 => {
                conn.run_with_metadata(
                    "RETURN 1;".to_string(),
                    None,
                    Some(Metadata::from(self.metadata.clone())),
                )
                .await?
            }
            _ => {
                return Err(Error::InvalidClientVersion {
                    version: conn.version(),
                })
            }
        };
        Success::try_from(response)?;
        let (response, _records) = match conn.version() {
            V1_0 | V2_0 | V3_0 => conn.pull_all().await?,
            V4_0 | V4_1 => {
                let pull_meta = Metadata::from_iter(vec![("n", -1)]);
                conn.pull(Some(pull_meta)).await?
            }
            _ => {
                return Err(Error::InvalidClientVersion {
                    version: conn.version(),
                })
            }
        };
        Success::try_from(response)?;
        Ok(conn)
    }
}

#[cfg(test)]
mod tests {
    use crate::BoltConnectionManager;
    use bolt_client::Metadata;
    use bolt_proto::message::Success;
    use bolt_proto::version::{V1_0, V2_0, V3_0, V4_0, V4_1};
    use bolt_proto::Value;
    use futures_util::future::join_all;
    use mobc::{Manager, Pool};
    use std::collections::HashMap;
    use std::convert::TryFrom;
    use std::env::var;
    use std::iter::FromIterator;

    async fn get_connection_manager(
        preferred_versions: [u32; 4],
        succeed: bool,
    ) -> BoltConnectionManager {
        let credentials = if succeed {
            var("BOLT_PASS").unwrap()
        } else {
            String::from("invalid")
        };

        BoltConnectionManager::new(
            var("BOLT_ADDR").unwrap(),
            var("BOLT_DOMAIN").ok(),
            preferred_versions,
            HashMap::from_iter(vec![
                ("user_agent", "bolt-client/X.Y.Z"),
                ("scheme", "basic"),
                ("principal", &var("BOLT_USER").unwrap()),
                ("credentials", &credentials),
            ]),
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn basic_pool() {
        for &bolt_version in &[V1_0, V2_0, V3_0, V4_0, V4_1] {
            let manager = get_connection_manager([bolt_version, 0, 0, 0], true).await;
            // Don't even test connection pool if server doesn't support this Bolt version
            if manager.connect().await.is_err() {
                println!(
                    "Skipping test: server doesn't support Bolt version {:#x}.",
                    bolt_version
                );
                continue;
            }
            let pool = Pool::builder().max_open(15).build(manager);

            let mut tasks = Vec::with_capacity(50);
            for i in 1..=tasks.capacity() {
                let pool = pool.clone();
                tasks.push(async move {
                    let mut client = pool.get().await.unwrap();
                    let statement = format!("RETURN {} as num;", i);
                    let version = client.version();
                    let (response, records) = match version {
                        V1_0 | V2_0 => {
                            client.run(statement, None).await.unwrap();
                            client.pull_all().await.unwrap()
                        }
                        V3_0 => {
                            client
                                .run_with_metadata(statement, None, None)
                                .await
                                .unwrap();
                            client.pull_all().await.unwrap()
                        }
                        V4_0 | V4_1 => {
                            client
                                .run_with_metadata(statement, None, None)
                                .await
                                .unwrap();
                            client
                                .pull(Some(Metadata::from_iter(vec![("n".to_string(), 1)])))
                                .await
                                .unwrap()
                        }
                        _ => panic!("Unsupported client version: {:#x}", version),
                    };
                    assert!(Success::try_from(response).is_ok());
                    assert_eq!(records[0].fields(), &[Value::from(i as i8)]);
                });
            }
            join_all(tasks).await;
        }
    }

    #[tokio::test]
    async fn invalid_init_fails() {
        let invalid_manager = get_connection_manager([V4_1, V4_0, V3_0, V2_0], false).await;
        let pool = Pool::builder().max_open(2).build(invalid_manager);
        let conn = pool.get().await;
        assert!(matches!(conn, Err(_)));
    }
}