Tweak connection pool API

This commit is contained in:
Steven Fackler 2014-04-11 19:45:06 -07:00
parent a1acc98f5b
commit 8516d841f1
3 changed files with 15 additions and 20 deletions

View File

@ -414,7 +414,7 @@ impl InnerPostgresConnection {
host,
port,
user,
mut path,
path,
query: mut args,
..
}: Url = match FromStr::from_str(url) {
@ -454,8 +454,10 @@ impl InnerPostgresConnection {
args.push((~"user", user.user.clone()));
if !path.is_empty() {
// path contains the leading /
path.shift_char();
args.push((~"database", path));
let mut path = StrBuf::from_owned_str(path);
// FIXME
unsafe { path.shift_byte(); }
args.push((~"database", path.into_owned()));
}
try_pg_conn!(conn.write_messages([StartupMessage {
version: message::PROTOCOL_VERSION,

View File

@ -25,7 +25,8 @@ impl Drop for InnerConnectionPool {
loop {
match self.pool.pop() {
Some(conn) => unsafe {
drop(cast::transmute::<*(), ~PostgresConnection>(conn));
let c: ~PostgresConnection = cast::transmute(conn);
drop(c);
},
None => break
}
@ -34,14 +35,9 @@ impl Drop for InnerConnectionPool {
}
impl InnerConnectionPool {
fn new_connection(&mut self) -> Option<PostgresConnectError> {
match PostgresConnection::connect(self.url, &self.ssl) {
Ok(conn) => {
unsafe { self.pool.push(cast::transmute(~conn)) };
None
}
Err(err) => Some(err)
}
fn add_connection(&mut self) -> Result<(), PostgresConnectError> {
PostgresConnection::connect(self.url, &self.ssl)
.map(|c| unsafe { self.pool.push(cast::transmute(~c)); })
}
}
@ -54,7 +50,7 @@ impl InnerConnectionPool {
/// ```rust,no_run
/// # use postgres::NoSsl;
/// # use postgres::pool::PostgresConnectionPool;
/// let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
/// let pool = PostgresConnectionPool::new(~"postgres://postgres@localhost",
/// NoSsl, 5).unwrap();
/// for _ in range(0, 10) {
/// let pool = pool.clone();
@ -74,19 +70,16 @@ impl PostgresConnectionPool {
///
/// Returns an error if the specified number of connections cannot be
/// created.
pub fn new(url: &str, ssl: SslMode, pool_size: uint)
pub fn new(url: ~str, ssl: SslMode, pool_size: uint)
-> Result<PostgresConnectionPool, PostgresConnectError> {
let mut pool = InnerConnectionPool {
url: url.to_owned(),
url: url,
ssl: ssl,
pool: Vec::new(),
};
for _ in range(0, pool_size) {
match pool.new_connection() {
None => (),
Some(err) => return Err(err)
}
try!(pool.add_connection());
}
Ok(PostgresConnectionPool {

View File

@ -49,7 +49,7 @@ macro_rules! or_fail(
#[test]
// Make sure we can take both connections at once and can still get one after
fn test_pool() {
let pool = or_fail!(PostgresConnectionPool::new("postgres://postgres@localhost",
let pool = or_fail!(PostgresConnectionPool::new(~"postgres://postgres@localhost",
NoSsl, 2));
let (stream1, stream2) = sync::duplex();