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
#![deny(missing_docs, missing_debug_implementations)]
pub use bb8;
pub use tokio_postgres;
use async_trait::async_trait;
use tokio_postgres::config::Config;
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
use tokio_postgres::{Client, Error, Socket};
use std::fmt;
use std::str::FromStr;
#[derive(Clone)]
pub struct PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
config: Config,
tls: Tls,
}
impl<Tls> PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
pub fn new(config: Config, tls: Tls) -> PostgresConnectionManager<Tls> {
PostgresConnectionManager { config, tls }
}
pub fn new_from_stringlike<T>(
params: T,
tls: Tls,
) -> Result<PostgresConnectionManager<Tls>, Error>
where
T: ToString,
{
let stringified_params = params.to_string();
let config = Config::from_str(&stringified_params)?;
Ok(Self::new(config, tls))
}
}
#[async_trait]
impl<Tls> bb8::ManageConnection for PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
<Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
<Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
<<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
{
type Connection = Client;
type Error = Error;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
let (client, connection) = self.config.connect(self.tls.clone()).await?;
tokio::spawn(async move { connection.await.map(|_| ()) });
Ok(client)
}
async fn is_valid(&self, conn: &mut Self::Connection) -> Result<(), Self::Error> {
conn.simple_query("").await.map(|_| ())
}
fn has_broken(&self, conn: &mut Self::Connection) -> bool {
conn.is_closed()
}
}
impl<Tls> fmt::Debug for PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PostgresConnectionManager")
.field("config", &self.config)
.finish()
}
}