From 2da41be9871e01495cb9b42609a12020b41e9629 Mon Sep 17 00:00:00 2001 From: Steven Fackler Date: Sat, 1 Nov 2014 16:22:41 -0700 Subject: [PATCH] Remove the old connection pool It's been deprecated for a long time --- src/lib.rs | 1 - src/pool.rs | 112 -------------------------------------------------- tests/pool.rs | 36 ---------------- tests/test.rs | 1 - 4 files changed, 150 deletions(-) delete mode 100644 src/pool.rs delete mode 100644 tests/pool.rs diff --git a/src/lib.rs b/src/lib.rs index bd87af7e..6f03b3f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -145,7 +145,6 @@ mod message; mod url; mod util; pub mod error; -pub mod pool; pub mod types; const CANARY: u32 = 0xdeadbeef; diff --git a/src/pool.rs b/src/pool.rs deleted file mode 100644 index 9c3d1dad..00000000 --- a/src/pool.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! A simple connection pool -#![deprecated="Use https://github.com/sfackler/r2d2-postgres instead"] -#![allow(deprecated)] - -use std::sync::{Arc, Mutex}; - -use {ConnectParams, IntoConnectParams, Connection, SslMode}; -use error::PostgresConnectError; - -struct InnerConnectionPool { - params: ConnectParams, - ssl: SslMode, - pool: Vec, -} - -impl InnerConnectionPool { - fn add_connection(&mut self) -> Result<(), PostgresConnectError> { - Connection::connect(self.params.clone(), &self.ssl) - .map(|c| self.pool.push(c)) - } -} - -/// A simple fixed-size Postgres connection pool. -/// -/// It can be shared across tasks. -/// -/// ## Example -/// -/// ```rust,no_run -/// # #![allow(deprecated)] -/// # use postgres::NoSsl; -/// # use postgres::pool::ConnectionPool; -/// let pool = ConnectionPool::new("postgres://postgres@localhost", -/// NoSsl, 5).unwrap(); -/// for _ in range(0u, 10) { -/// let pool = pool.clone(); -/// spawn(proc() { -/// let conn = pool.get_connection(); -/// conn.execute("UPDATE foo SET bar = 1", []).unwrap(); -/// }); -/// } -/// ``` -#[deriving(Clone)] -pub struct ConnectionPool { - pool: Arc> -} - -impl ConnectionPool { - /// Creates a new pool with the specified number of connections. - /// - /// Returns an error if the specified number of connections cannot be - /// created. - pub fn new(params: T, ssl: SslMode, pool_size: uint) - -> Result { - let mut pool = InnerConnectionPool { - params: try!(params.into_connect_params()), - ssl: ssl, - pool: vec![], - }; - - for _ in range(0, pool_size) { - try!(pool.add_connection()); - } - - Ok(ConnectionPool { - pool: Arc::new(Mutex::new(pool)) - }) - } - - /// Retrieves a connection from the pool. - /// - /// If all connections are in use, blocks until one becomes available. - pub fn get_connection(&self) -> PooledConnection { - let mut pool = self.pool.lock(); - - loop { - match pool.pool.pop() { - Some(conn) => { - return PooledConnection { - pool: self.clone(), - conn: Some(conn), - }; - } - None => pool.cond.wait() - } - } - } -} - -/// A Postgres connection pulled from a connection pool. -/// -/// It will be returned to the pool when it falls out of scope, even due to -/// task failure. -pub struct PooledConnection { - pool: ConnectionPool, - // TODO remove the Option wrapper when drop takes self by value - conn: Option -} - -impl Drop for PooledConnection { - fn drop(&mut self) { - let mut pool = self.pool.pool.lock(); - pool.pool.push(self.conn.take().unwrap()); - pool.cond.signal(); - } -} - -impl Deref for PooledConnection { - fn deref<'a>(&'a self) -> &'a Connection { - self.conn.as_ref().unwrap() - } -} diff --git a/tests/pool.rs b/tests/pool.rs deleted file mode 100644 index 293b6102..00000000 --- a/tests/pool.rs +++ /dev/null @@ -1,36 +0,0 @@ -#![allow(deprecated)] - -use std::comm; -use std::sync::Future; - -use postgres::NoSsl; -use postgres::pool::ConnectionPool; - -#[test] -// Make sure we can take both connections at once and can still get one after -fn test_pool() { - let pool = or_panic!(ConnectionPool::new("postgres://postgres@localhost", - NoSsl, 2)); - - let (s1, r1) = comm::channel(); - let (s2, r2) = comm::channel(); - - let pool1 = pool.clone(); - let mut fut1 = Future::spawn(proc() { - let _conn = pool1.get_connection(); - s1.send(()); - r2.recv(); - }); - - let pool2 = pool.clone(); - let mut fut2 = Future::spawn(proc() { - let _conn = pool2.get_connection(); - s2.send(()); - r1.recv(); - }); - - fut1.get(); - fut2.get(); - - pool.get_connection(); -} diff --git a/tests/test.rs b/tests/test.rs index db8882aa..06e5c8ae 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -48,7 +48,6 @@ macro_rules! or_panic( ) mod types; -mod pool; #[test] fn test_non_default_database() {