Remove the old connection pool
It's been deprecated for a long time
This commit is contained in:
parent
c11cbd6b7d
commit
2da41be987
@ -145,7 +145,6 @@ mod message;
|
|||||||
mod url;
|
mod url;
|
||||||
mod util;
|
mod util;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod pool;
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
const CANARY: u32 = 0xdeadbeef;
|
const CANARY: u32 = 0xdeadbeef;
|
||||||
|
112
src/pool.rs
112
src/pool.rs
@ -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<Connection>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Mutex<InnerConnectionPool>>
|
|
||||||
}
|
|
||||||
|
|
||||||
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<T: IntoConnectParams>(params: T, ssl: SslMode, pool_size: uint)
|
|
||||||
-> Result<ConnectionPool, PostgresConnectError> {
|
|
||||||
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<Connection>
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Connection> for PooledConnection {
|
|
||||||
fn deref<'a>(&'a self) -> &'a Connection {
|
|
||||||
self.conn.as_ref().unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
@ -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();
|
|
||||||
}
|
|
@ -48,7 +48,6 @@ macro_rules! or_panic(
|
|||||||
)
|
)
|
||||||
|
|
||||||
mod types;
|
mod types;
|
||||||
mod pool;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_non_default_database() {
|
fn test_non_default_database() {
|
||||||
|
Loading…
Reference in New Issue
Block a user