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
#![deny(missing_docs, missing_debug_implementations)]
pub extern crate bb8;
pub extern crate tokio_postgres;
extern crate futures;
use futures::prelude::*;
use tokio_postgres::tls::{MakeTlsConnect, TlsConnect};
use tokio_postgres::{Client, Error, Socket};
use std::fmt;
#[derive(Clone)]
pub struct PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
params: String,
tls: Tls,
}
impl<Tls> PostgresConnectionManager<Tls>
where
Tls: MakeTlsConnect<Socket>,
{
pub fn new<T>(params: T, tls: Tls) -> PostgresConnectionManager<Tls>
where
T: ToString,
{
PostgresConnectionManager {
params: params.to_string(),
tls: tls,
}
}
}
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;
fn connect(
&self,
) -> Box<Future<Item = Self::Connection, Error = Self::Error> + Send + 'static> {
Box::new(tokio_postgres::connect(&self.params, self.tls.clone()).map(
|(client, connection)| {
tokio::spawn(connection.map_err(|_| panic!()));
client
},
))
}
fn is_valid(
&self,
mut conn: Self::Connection,
) -> Box<Future<Item = Self::Connection, Error = (Self::Error, Self::Connection)> + Send> {
let f = conn.simple_query("").collect();
Box::new(f.then(move |r| match r {
Ok(_) => Ok(conn),
Err(e) => Err((e, conn)),
}))
}
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("params", &self.params)
.finish()
}
}