mirror of
https://github.com/chatmail/core.git
synced 2026-04-22 16:06:30 +03:00
array backed pool
This commit is contained in:
10
src/sql.rs
10
src/sql.rs
@@ -196,12 +196,12 @@ impl Sql {
|
|||||||
|
|
||||||
/// Creates a new connection pool.
|
/// Creates a new connection pool.
|
||||||
fn new_pool(dbfile: &Path, passphrase: String) -> Result<Pool> {
|
fn new_pool(dbfile: &Path, passphrase: String) -> Result<Pool> {
|
||||||
let mut connections = Vec::new();
|
let mut connections = [None, None, None]; // array from iter is not stable yet
|
||||||
for _ in 0..3 {
|
|
||||||
let connection = new_connection(dbfile, &passphrase)?;
|
|
||||||
connections.push(connection);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
for c in &mut connections {
|
||||||
|
let connection = new_connection(dbfile, &passphrase)?;
|
||||||
|
*c = Some(connection);
|
||||||
|
}
|
||||||
let pool = Pool::new(connections);
|
let pool = Pool::new(connections);
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,10 +7,13 @@ use std::sync::{Arc, Weak};
|
|||||||
use parking_lot::{Condvar, Mutex};
|
use parking_lot::{Condvar, Mutex};
|
||||||
use rusqlite::Connection;
|
use rusqlite::Connection;
|
||||||
|
|
||||||
|
/// Total size of the connection pool.
|
||||||
|
pub const POOL_SIZE: usize = 3;
|
||||||
|
|
||||||
/// Inner connection pool.
|
/// Inner connection pool.
|
||||||
struct InnerPool {
|
struct InnerPool {
|
||||||
/// Available connections.
|
/// Available connections.
|
||||||
connections: Mutex<Vec<Connection>>,
|
connections: Mutex<[Option<Connection>; POOL_SIZE]>,
|
||||||
|
|
||||||
/// Conditional variable to notify about added connections.
|
/// Conditional variable to notify about added connections.
|
||||||
///
|
///
|
||||||
@@ -24,7 +27,19 @@ impl InnerPool {
|
|||||||
/// The connection could be new or returned back.
|
/// The connection could be new or returned back.
|
||||||
fn put(&self, connection: Connection) {
|
fn put(&self, connection: Connection) {
|
||||||
let mut connections = self.connections.lock();
|
let mut connections = self.connections.lock();
|
||||||
connections.push(connection);
|
let mut found_one = false;
|
||||||
|
for c in &mut *connections {
|
||||||
|
if c.is_none() {
|
||||||
|
*c = Some(connection);
|
||||||
|
found_one = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert!(
|
||||||
|
found_one,
|
||||||
|
"attempted to put more connections than available"
|
||||||
|
);
|
||||||
|
|
||||||
drop(connections);
|
drop(connections);
|
||||||
self.cond.notify_one();
|
self.cond.notify_one();
|
||||||
}
|
}
|
||||||
@@ -79,7 +94,7 @@ impl fmt::Debug for Pool {
|
|||||||
|
|
||||||
impl Pool {
|
impl Pool {
|
||||||
/// Creates a new connection pool.
|
/// Creates a new connection pool.
|
||||||
pub fn new(connections: Vec<Connection>) -> Self {
|
pub fn new(connections: [Option<Connection>; POOL_SIZE]) -> Self {
|
||||||
let inner = Arc::new(InnerPool {
|
let inner = Arc::new(InnerPool {
|
||||||
connections: Mutex::new(connections),
|
connections: Mutex::new(connections),
|
||||||
cond: Condvar::new(),
|
cond: Condvar::new(),
|
||||||
@@ -92,7 +107,7 @@ impl Pool {
|
|||||||
let mut connections = self.inner.connections.lock();
|
let mut connections = self.inner.connections.lock();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(conn) = connections.pop() {
|
if let Some(conn) = get_next(&mut *connections) {
|
||||||
return PooledConnection {
|
return PooledConnection {
|
||||||
pool: Arc::downgrade(&self.inner),
|
pool: Arc::downgrade(&self.inner),
|
||||||
conn: Some(conn),
|
conn: Some(conn),
|
||||||
@@ -103,3 +118,16 @@ impl Pool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the first available connection.
|
||||||
|
///
|
||||||
|
/// `None` if no connection is availble.
|
||||||
|
fn get_next(connections: &mut [Option<Connection>; POOL_SIZE]) -> Option<Connection> {
|
||||||
|
for c in &mut *connections {
|
||||||
|
if c.is_some() {
|
||||||
|
return c.take();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user