|
1 | 1 | //! Postgres support for the `r2d2` connection pool. |
2 | | -#![doc(html_root_url="https://docs.rs/r2d2_postgres/0.14")] |
| 2 | +#![doc(html_root_url = "https://docs.rs/r2d2_postgres/0.15.0-rc.1")] |
3 | 3 | #![warn(missing_docs)] |
4 | | -pub use r2d2; |
5 | 4 | pub use postgres; |
| 5 | +pub use r2d2; |
6 | 6 |
|
7 | | -use postgres::{Connection, Error, Result}; |
8 | | -use postgres::params::{ConnectParams, IntoConnectParams}; |
9 | | -use postgres::tls::TlsHandshake; |
| 7 | +use postgres::tls::{MakeTlsConnect, TlsConnect}; |
| 8 | +use postgres::{Client, Config, Error}; |
| 9 | +use r2d2::ManageConnection; |
| 10 | +use tokio_postgres::Socket; |
10 | 11 |
|
11 | | -/// Like `postgres::TlsMode` except that it owns its `TlsHandshake` instance. |
12 | | -#[derive(Debug)] |
13 | | -pub enum TlsMode { |
14 | | - /// Like `postgres::TlsMode::None`. |
15 | | - None, |
16 | | - /// Like `postgres::TlsMode::Prefer`. |
17 | | - Prefer(Box<dyn TlsHandshake + Sync + Send>), |
18 | | - /// Like `postgres::TlsMode::Require`. |
19 | | - Require(Box<dyn TlsHandshake + Sync + Send>), |
20 | | -} |
21 | | - |
22 | | -/// An `r2d2::ManageConnection` for `postgres::Connection`s. |
| 12 | +/// An `r2d2::ManageConnection` for `postgres::Client`s. |
23 | 13 | /// |
24 | 14 | /// ## Example |
25 | 15 | /// |
26 | | -/// ```rust,no_run |
27 | | -/// extern crate r2d2; |
28 | | -/// extern crate r2d2_postgres; |
29 | | -/// |
| 16 | +/// ```no_run |
30 | 17 | /// use std::thread; |
31 | | -/// use r2d2_postgres::{TlsMode, PostgresConnectionManager}; |
| 18 | +/// use postgres::{NoTls, Client}; |
| 19 | +/// use r2d2_postgres::PostgresConnectionManager; |
32 | 20 | /// |
33 | 21 | /// fn main() { |
34 | | -/// let manager = PostgresConnectionManager::new("postgres://postgres@localhost", |
35 | | -/// TlsMode::None).unwrap(); |
| 22 | +/// let manager = PostgresConnectionManager::new( |
| 23 | +/// "host=localhost user=postgres".parse().unwrap(), |
| 24 | +/// NoTls, |
| 25 | +/// ); |
36 | 26 | /// let pool = r2d2::Pool::new(manager).unwrap(); |
37 | 27 | /// |
38 | 28 | /// for i in 0..10i32 { |
39 | 29 | /// let pool = pool.clone(); |
40 | 30 | /// thread::spawn(move || { |
41 | | -/// let conn = pool.get().unwrap(); |
42 | | -/// conn.execute("INSERT INTO foo (bar) VALUES ($1)", &[&i]).unwrap(); |
| 31 | +/// let mut client = pool.get().unwrap(); |
| 32 | +/// client.execute("INSERT INTO foo (bar) VALUES ($1)", &[&i]).unwrap(); |
43 | 33 | /// }); |
44 | 34 | /// } |
45 | 35 | /// } |
46 | 36 | /// ``` |
47 | 37 | #[derive(Debug)] |
48 | | -pub struct PostgresConnectionManager { |
49 | | - params: ConnectParams, |
50 | | - ssl_mode: TlsMode, |
| 38 | +pub struct PostgresConnectionManager<T> { |
| 39 | + config: Config, |
| 40 | + tls_connector: T, |
51 | 41 | } |
52 | 42 |
|
53 | | -impl PostgresConnectionManager { |
| 43 | +impl<T> PostgresConnectionManager<T> |
| 44 | +where |
| 45 | + T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send, |
| 46 | + T::TlsConnect: Send, |
| 47 | + T::Stream: Send, |
| 48 | + <T::TlsConnect as TlsConnect<Socket>>::Future: Send, |
| 49 | +{ |
54 | 50 | /// Creates a new `PostgresConnectionManager`. |
55 | | - /// |
56 | | - /// See `postgres::Connection::connect` for a description of the parameter |
57 | | - /// types. |
58 | | - pub fn new<T>(params: T, |
59 | | - ssl_mode: TlsMode) |
60 | | - -> Result<PostgresConnectionManager> |
61 | | - where T: IntoConnectParams |
62 | | - { |
63 | | - // FIXME we shouldn't be using this private constructor :( |
64 | | - let params = params.into_connect_params().map_err(postgres_shared::error::connect)?; |
65 | | - |
66 | | - Ok(PostgresConnectionManager { |
67 | | - params: params, |
68 | | - ssl_mode: ssl_mode, |
69 | | - }) |
| 51 | + pub fn new(config: Config, tls_connector: T) -> PostgresConnectionManager<T> { |
| 52 | + PostgresConnectionManager { |
| 53 | + config, |
| 54 | + tls_connector, |
| 55 | + } |
70 | 56 | } |
71 | 57 | } |
72 | 58 |
|
73 | | -impl r2d2::ManageConnection for PostgresConnectionManager { |
74 | | - type Connection = Connection; |
| 59 | +impl<T> ManageConnection for PostgresConnectionManager<T> |
| 60 | +where |
| 61 | + T: MakeTlsConnect<Socket> + Clone + 'static + Sync + Send, |
| 62 | + T::TlsConnect: Send, |
| 63 | + T::Stream: Send, |
| 64 | + <T::TlsConnect as TlsConnect<Socket>>::Future: Send, |
| 65 | +{ |
| 66 | + type Connection = Client; |
75 | 67 | type Error = Error; |
76 | 68 |
|
77 | | - fn connect(&self) -> Result<postgres::Connection> { |
78 | | - let mode = match self.ssl_mode { |
79 | | - TlsMode::None => postgres::TlsMode::None, |
80 | | - TlsMode::Prefer(ref n) => postgres::TlsMode::Prefer(&**n), |
81 | | - TlsMode::Require(ref n) => postgres::TlsMode::Require(&**n), |
82 | | - }; |
83 | | - postgres::Connection::connect(self.params.clone(), mode) |
| 69 | + fn connect(&self) -> Result<Client, Error> { |
| 70 | + self.config.connect(self.tls_connector.clone()) |
84 | 71 | } |
85 | 72 |
|
86 | | - fn is_valid(&self, conn: &mut Connection) -> Result<()> { |
87 | | - conn.batch_execute("") |
| 73 | + fn is_valid(&self, client: &mut Client) -> Result<(), Error> { |
| 74 | + client.simple_query("").map(|_| ()) |
88 | 75 | } |
89 | 76 |
|
90 | | - fn has_broken(&self, conn: &mut Connection) -> bool { |
91 | | - conn.is_desynchronized() |
| 77 | + fn has_broken(&self, client: &mut Client) -> bool { |
| 78 | + client.is_closed() |
92 | 79 | } |
93 | 80 | } |
0 commit comments