@@ -9,39 +9,16 @@ use super::{PostgresConnection,
99 PostgresTransaction } ;
1010use super :: types:: ToSql ;
1111
12- pub struct PostgresConnectionPoolConfig {
13- initial_size : uint ,
14- min_size : uint ,
15- max_size : uint
16- }
17-
18- impl PostgresConnectionPoolConfig {
19- fn validate ( & self ) {
20- assert ! ( self . initial_size >= self . min_size) ;
21- assert ! ( self . initial_size <= self . max_size) ;
22- }
23- }
24-
25- pub static DEFAULT_CONFIG : PostgresConnectionPoolConfig =
26- PostgresConnectionPoolConfig {
27- initial_size : 3 ,
28- min_size : 3 ,
29- max_size : 15
30- } ;
31-
3212struct InnerConnectionPool {
3313 url : ~str ,
34- config : PostgresConnectionPoolConfig ,
3514 pool : ~[ PostgresConnection ] ,
36- size : uint ,
3715}
3816
3917impl InnerConnectionPool {
4018 fn new_connection ( & mut self ) -> Option < PostgresConnectError > {
4119 match PostgresConnection :: try_connect ( self . url ) {
4220 Ok ( conn) => {
4321 self . pool . push ( conn) ;
44- self . size += 1 ;
4522 None
4623 }
4724 Err ( err) => Some ( err)
@@ -56,18 +33,14 @@ pub struct PostgresConnectionPool {
5633}
5734
5835impl PostgresConnectionPool {
59- pub fn new ( url : & str , config : PostgresConnectionPoolConfig )
36+ pub fn try_new ( url : & str , pool_size : uint )
6037 -> Result < PostgresConnectionPool , PostgresConnectError > {
61- config. validate ( ) ;
62-
6338 let mut pool = InnerConnectionPool {
6439 url : url. to_owned ( ) ,
65- config : config,
6640 pool : ~[ ] ,
67- size : 0 ,
6841 } ;
6942
70- while pool. size < pool . config . initial_size {
43+ while pool. pool . len ( ) < pool_size {
7144 match pool. new_connection ( ) {
7245 None => ( ) ,
7346 Some ( err) => return Err ( err)
@@ -79,6 +52,13 @@ impl PostgresConnectionPool {
7952 } )
8053 }
8154
55+ pub fn new ( url : & str , pool_size : uint ) -> PostgresConnectionPool {
56+ match PostgresConnectionPool :: try_new ( url, pool_size) {
57+ Ok ( pool) => pool,
58+ Err ( err) => fail ! ( "Unable to initialize pool: %s" , err. to_str( ) )
59+ }
60+ }
61+
8262 pub fn try_get_connection ( & self ) -> Result < PooledPostgresConnection ,
8363 PostgresConnectError > {
8464 let conn = unsafe {
@@ -108,7 +88,7 @@ impl PostgresConnectionPool {
10888// Should be a newtype
10989pub struct PooledPostgresConnection {
11090 priv pool : PostgresConnectionPool ,
111- // Todo remove the Option wrapper when drop takes self by value
91+ // TODO remove the Option wrapper when drop takes self by value
11292 priv conn : Option < PostgresConnection >
11393}
11494
0 commit comments