Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add PostgresPoolManager
- Loading branch information
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
target/ | ||
Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "r2d2_postgres" | ||
version = "0.0.0" | ||
authors = ["Steven Fackler <sfackler@gmail.com>"] | ||
|
||
[[lib]] | ||
name = "r2d2_postgres" | ||
path = "src/lib.rs" | ||
test = false | ||
|
||
[[test]] | ||
name = "test" | ||
path = "tests/test.rs" | ||
|
||
[dependencies.rust-postgres] | ||
git = "https://github.com/sfackler/rust-postgres" | ||
rev = "7d8424418c52e148cba4bcfd89eeb9b5abdf7573" | ||
|
||
[dependencies.r2d2] | ||
git = "https://github.com/sfackler/r2d2" | ||
rev = "150f55e4f5cc73a554be72e18c5ed61184673043" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
extern crate r2d2; | ||
extern crate postgres; | ||
|
||
use postgres::{PostgresConnection, PostgresConnectParams, IntoConnectParams, SslMode}; | ||
use postgres::error::PostgresConnectError; | ||
|
||
pub struct PostgresPoolManager { | ||
params: PostgresConnectParams, | ||
ssl_mode: SslMode, | ||
} | ||
|
||
impl PostgresPoolManager { | ||
pub fn new<T: IntoConnectParams>(params: T, ssl_mode: SslMode) | ||
-> Result<PostgresPoolManager, PostgresConnectError> { | ||
Ok(PostgresPoolManager { | ||
params: try!(params.into_connect_params()), | ||
ssl_mode: ssl_mode, | ||
}) | ||
} | ||
} | ||
|
||
impl r2d2::PoolManager<PostgresConnection, PostgresConnectError> for PostgresPoolManager { | ||
fn connect(&self) -> Result<PostgresConnection, PostgresConnectError> { | ||
PostgresConnection::connect(self.params.clone(), &self.ssl_mode) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
extern crate postgres; | ||
extern crate r2d2; | ||
extern crate r2d2_postgres; | ||
|
||
use std::default::Default; | ||
use std::sync::{Arc, Future}; | ||
use std::comm; | ||
|
||
use postgres::NoSsl; | ||
use postgres::error::SocketError; | ||
use r2d2_postgres::PostgresPoolManager; | ||
|
||
#[test] | ||
fn test_bad_url_error() { | ||
let manager = PostgresPoolManager::new("postgres://bogushost", NoSsl).unwrap(); | ||
let config = Default::default(); | ||
match r2d2::Pool::new(config, manager) { | ||
Err(r2d2::ConnectionError(SocketError(_))) => {} | ||
Err(err) => fail!("Unexpected error {}", err), | ||
_ => fail!("Unexpected success") | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_basic() { | ||
let manager = PostgresPoolManager::new("postgres://postgres@localhost", NoSsl).unwrap(); | ||
let config = r2d2::Config { | ||
initial_size: 2, | ||
..Default::default() | ||
}; | ||
let pool = Arc::new(r2d2::Pool::new(config, manager).unwrap()); | ||
|
||
let (s1, r1) = comm::channel(); | ||
let (s2, r2) = comm::channel(); | ||
|
||
let pool1 = pool.clone(); | ||
let mut fut1 = Future::spawn(proc() { | ||
let conn = pool1.get().unwrap(); | ||
s1.send(()); | ||
r2.recv(); | ||
conn.replace(); | ||
}); | ||
|
||
let pool2 = pool.clone(); | ||
let mut fut2 = Future::spawn(proc() { | ||
let conn = pool2.get().unwrap(); | ||
s2.send(()); | ||
r1.recv(); | ||
conn.replace(); | ||
}); | ||
|
||
fut1.get(); | ||
fut2.get(); | ||
|
||
pool.get().unwrap().replace(); | ||
} |