|
| 1 | +extern mod extra; |
| 2 | + |
| 3 | +use extra::arc::MutexArc; |
| 4 | +use std::cell::Cell; |
| 5 | + |
| 6 | +use super::{PostgresConnection, PostgresConnectError}; |
| 7 | + |
| 8 | +pub struct PostgresConnectionPoolConfig { |
| 9 | + initial_size: uint, |
| 10 | + min_size: uint, |
| 11 | + max_size: uint |
| 12 | +} |
| 13 | + |
| 14 | +impl PostgresConnectionPoolConfig { |
| 15 | + fn validate(&self) { |
| 16 | + assert!(self.initial_size >= self.min_size); |
| 17 | + assert!(self.initial_size <= self.max_size); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +pub static DEFAULT_CONFIG: PostgresConnectionPoolConfig = |
| 22 | + PostgresConnectionPoolConfig { |
| 23 | + initial_size: 3, |
| 24 | + min_size: 3, |
| 25 | + max_size: 15 |
| 26 | + }; |
| 27 | + |
| 28 | +struct InnerConnectionPool { |
| 29 | + url: ~str, |
| 30 | + config: PostgresConnectionPoolConfig, |
| 31 | + pool: ~[PostgresConnection], |
| 32 | + size: uint, |
| 33 | +} |
| 34 | + |
| 35 | +impl InnerConnectionPool { |
| 36 | + fn new_connection(&mut self) -> Option<PostgresConnectError> { |
| 37 | + match PostgresConnection::try_connect(self.url) { |
| 38 | + Ok(conn) => { |
| 39 | + self.pool.push(conn); |
| 40 | + self.size += 1; |
| 41 | + None |
| 42 | + } |
| 43 | + Err(err) => Some(err) |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// Should be a newtype, but blocked by mozilla/rust#9155 |
| 49 | +pub struct PostgresConnectionPool { |
| 50 | + pool: MutexArc<InnerConnectionPool> |
| 51 | +} |
| 52 | + |
| 53 | +impl PostgresConnectionPool { |
| 54 | + pub fn new(url: &str, config: PostgresConnectionPoolConfig) |
| 55 | + -> Result<PostgresConnectionPool, PostgresConnectError> { |
| 56 | + config.validate(); |
| 57 | + |
| 58 | + let mut pool = InnerConnectionPool { |
| 59 | + url: url.to_owned(), |
| 60 | + config: config, |
| 61 | + pool: ~[], |
| 62 | + size: 0, |
| 63 | + }; |
| 64 | + |
| 65 | + while pool.size < pool.config.initial_size { |
| 66 | + match pool.new_connection() { |
| 67 | + None => (), |
| 68 | + Some(err) => return Err(err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + Ok(PostgresConnectionPool { |
| 73 | + pool: MutexArc::new(pool) |
| 74 | + }) |
| 75 | + } |
| 76 | + |
| 77 | + pub fn try_with_connection<T>(&mut self, |
| 78 | + blk: &fn(&PostgresConnection) -> T) |
| 79 | + -> Result<T, PostgresConnectError> { |
| 80 | + let conn = unsafe { |
| 81 | + do self.pool.unsafe_access_cond |pool, cond| { |
| 82 | + while pool.pool.is_empty() { |
| 83 | + cond.wait(); |
| 84 | + } |
| 85 | + |
| 86 | + pool.pool.pop() |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + let ret = blk(&conn); |
| 91 | + |
| 92 | + let conn = Cell::new(conn); |
| 93 | + unsafe { |
| 94 | + do self.pool.unsafe_access |pool| { |
| 95 | + pool.pool.push(conn.take()); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + Ok(ret) |
| 100 | + } |
| 101 | + |
| 102 | + pub fn with_connection<T>(&mut self, blk: &fn(&PostgresConnection) -> T) |
| 103 | + -> T { |
| 104 | + match self.try_with_connection(blk) { |
| 105 | + Ok(ret) => ret, |
| 106 | + Err(err) => fail!("Error getting connection: %s", err.to_str()) |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments