Skip to content

Commit 498d2e7

Browse files
committed
Simplify connection pool
It's going to just be fixed size for now
1 parent fa472cf commit 498d2e7

3 files changed

Lines changed: 29 additions & 38 deletions

File tree

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,23 @@ match conn.try_update(query, params) {
171171
}
172172
```
173173

174+
Connection Pooling
175+
------------------
176+
A very basic fixed-size connection pool is provided in the `pool` module. A
177+
single pool can be shared across tasks and `get_connection` will block until a
178+
connection is available.
179+
```rust
180+
let pool = PostgresConnectionPool::new("postgres://postgres@localhost", 5)
181+
.unwrap();
182+
183+
for _ in range(0, 10) {
184+
do task::spawn_with(pool.clone()) |pool| {
185+
let conn = pool.get_connection();
186+
conn.query(...);
187+
}
188+
}
189+
```
190+
174191
Type Correspondence
175192
-------------------
176193
Rust-Postgres enforces a strict correspondence between Rust types and Postgres

src/pool/mod.rs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,39 +9,16 @@ use super::{PostgresConnection,
99
PostgresTransaction};
1010
use 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-
3212
struct InnerConnectionPool {
3313
url: ~str,
34-
config: PostgresConnectionPoolConfig,
3514
pool: ~[PostgresConnection],
36-
size: uint,
3715
}
3816

3917
impl 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

5835
impl 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
10989
pub 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

src/pool/test.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,12 @@ use std::cell::Cell;
55
use extra::comm::DuplexStream;
66
use extra::future;
77

8-
use postgres::pool::{PostgresConnectionPool, PostgresConnectionPoolConfig};
8+
use postgres::pool::PostgresConnectionPool;
99

1010
#[test]
1111
// Make sure we can take both connections at once and can still get one after
1212
fn test_pool() {
13-
let config = PostgresConnectionPoolConfig {
14-
initial_size: 2,
15-
min_size: 2,
16-
max_size: 2
17-
};
18-
let pool = PostgresConnectionPool::new("postgres://postgres@localhost",
19-
config).unwrap();
13+
let pool = PostgresConnectionPool::new("postgres://postgres@localhost", 2);
2014

2115
let (stream1, stream2) = DuplexStream::<(), ()>();
2216

0 commit comments

Comments
 (0)