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
use crate::{
connstring::ConnectionString, db::ThreadedDatabase, Client, ClientOptions, ThreadedClient,
};
#[derive(Debug)]
pub struct MongoConnectionManager {
conn_str: ConnectionString,
db_name: String,
client_options: Option<ClientOptions>,
}
impl MongoConnectionManager {
pub fn new<S, CO>(connection_str: ConnectionString, db_name: S, client_options: CO) -> Self
where
S: Into<String>,
CO: Into<Option<ClientOptions>>,
{
Self {
conn_str: connection_str,
db_name: db_name.into(),
client_options: client_options.into(),
}
}
}
impl r2d2::ManageConnection for MongoConnectionManager {
type Connection = crate::db::Database;
type Error = crate::error::Error;
fn connect(&self) -> Result<Self::Connection, Self::Error> {
let client = Client::with_config(self.conn_str.clone(), self.client_options.clone(), None)?;
client.acquire_stream(client.read_preference.clone())?;
Ok(client.db(&self.db_name))
}
fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.version()?;
Ok(())
}
fn has_broken(&self, _: &mut Self::Connection) -> bool {
false
}
}