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
use async_trait::async_trait;
use mobc::Manager;
use orma::tls::{MakeTlsConnect, TlsConnect};
use orma::{Config, Connection, DbError, Socket};
#[allow(clippy::needless_doctest_main)]
pub struct PgConnectionManager<Tls> {
config: Config,
tls: Tls,
}
impl<Tls> PgConnectionManager<Tls> {
pub fn new(config: Config, tls: Tls) -> Self {
Self { config, tls }
}
}
#[async_trait]
impl<Tls> Manager for PgConnectionManager<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 = Connection;
type Error = DbError;
async fn connect(&self) -> Result<Self::Connection, Self::Error> {
let tls = self.tls.clone();
let (client, conn) = self.config.connect(tls).await?;
mobc::spawn(conn);
Ok(client.into())
}
async fn check(&self, conn: Self::Connection) -> Result<Self::Connection, Self::Error> {
conn.simple_query("").await?;
Ok(conn)
}
}