1#![warn(missing_docs)]
3pub use postgres;
4pub use r2d2;
5
6use postgres::tls::{MakeTlsConnect, TlsConnect};
7use postgres::{Client, Config, Error, Socket};
8use r2d2::ManageConnection;
9
10#[derive(Debug)]
35pub struct PostgresConnectionManager<T> {
36 config: Config,
37 tls_connector: T,
38}
39
40impl<T> PostgresConnectionManager<T>
41where
42 T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send,
43 T::TlsConnect: Send,
44 T::Stream: Send,
45 <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
46{
47 pub fn new(config: Config, tls_connector: T) -> PostgresConnectionManager<T> {
49 PostgresConnectionManager {
50 config,
51 tls_connector,
52 }
53 }
54}
55
56impl<T> ManageConnection for PostgresConnectionManager<T>
57where
58 T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send,
59 T::TlsConnect: Send,
60 T::Stream: Send,
61 <T::TlsConnect as TlsConnect<Socket>>::Future: Send,
62{
63 type Connection = Client;
64 type Error = Error;
65
66 fn connect(&self) -> Result<Client, Error> {
67 self.config.connect(self.tls_connector.clone())
68 }
69
70 fn is_valid(&self, client: &mut Client) -> Result<(), Error> {
71 client.simple_query("").map(|_| ())
72 }
73
74 fn has_broken(&self, client: &mut Client) -> bool {
75 client.is_closed()
76 }
77}