Skip to content

Commit ddb334c

Browse files
authored
Merge pull request #1 from zainkabani/zain/add-queue-strategy-option
Add LIFO option for getting resources in the pool
2 parents 040b7dc + 1780122 commit ddb334c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

bb8/src/api.rs

+20
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ impl<M: ManageConnection> Pool<M> {
7474
}
7575
}
7676

77+
#[derive(Debug)]
78+
pub enum QueueStrategy {
79+
/// Last in first out
80+
LIFO,
81+
/// First in first out
82+
FIFO,
83+
}
84+
7785
/// A builder for a connection pool.
7886
#[derive(Debug)]
7987
pub struct Builder<M: ManageConnection> {
@@ -95,6 +103,8 @@ pub struct Builder<M: ManageConnection> {
95103
pub(crate) error_sink: Box<dyn ErrorSink<M::Error>>,
96104
/// The time interval used to wake up and reap connections.
97105
pub(crate) reaper_rate: Duration,
106+
/// Queue strategy (FIFO or LIFO)
107+
pub(crate) queue_strategy: QueueStrategy,
98108
/// User-supplied trait object responsible for initializing connections
99109
pub(crate) connection_customizer: Option<Box<dyn CustomizeConnection<M::Connection, M::Error>>>,
100110
_p: PhantomData<M>,
@@ -112,6 +122,7 @@ impl<M: ManageConnection> Default for Builder<M> {
112122
retry_connection: true,
113123
error_sink: Box::new(NopErrorSink),
114124
reaper_rate: Duration::from_secs(30),
125+
queue_strategy: QueueStrategy::FIFO,
115126
connection_customizer: None,
116127
_p: PhantomData,
117128
}
@@ -260,6 +271,15 @@ impl<M: ManageConnection> Builder<M> {
260271
self
261272
}
262273

274+
/// Sets the queue strategy to be used by the pool
275+
///
276+
/// Defaults to `FIFO`.
277+
#[must_use]
278+
pub fn queue_strategy(mut self, queue_strategy: QueueStrategy) -> Self {
279+
self.queue_strategy = queue_strategy;
280+
self
281+
}
282+
263283
/// Set the connection customizer to customize newly checked out connections
264284
#[must_use]
265285
pub fn connection_customizer(

bb8/src/internals.rs

+11-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::cmp::min;
22
use std::sync::Arc;
33
use std::time::Instant;
44

5-
use crate::lock::Mutex;
5+
use crate::{api::QueueStrategy, lock::Mutex};
66
use futures_channel::oneshot;
77

88
use crate::api::{Builder, ManageConnection};
@@ -42,6 +42,7 @@ where
4242
conns: VecDeque<IdleConn<M::Connection>>,
4343
num_conns: u32,
4444
pending_conns: u32,
45+
queue_strategy: QueueStrategy,
4546
}
4647

4748
impl<M> PoolInternals<M>
@@ -80,8 +81,14 @@ where
8081
}
8182

8283
// Queue it in the idle queue
83-
self.conns
84-
.push_back(IdleConn::from(guard.conn.take().unwrap()));
84+
match self.queue_strategy {
85+
QueueStrategy::FIFO => self
86+
.conns
87+
.push_back(IdleConn::from(guard.conn.take().unwrap())),
88+
QueueStrategy::LIFO => self
89+
.conns
90+
.push_front(IdleConn::from(guard.conn.take().unwrap())),
91+
}
8592
}
8693

8794
pub(crate) fn connect_failed(&mut self, _: Approval) {
@@ -163,6 +170,7 @@ where
163170
conns: VecDeque::new(),
164171
num_conns: 0,
165172
pending_conns: 0,
173+
queue_strategy: QueueStrategy::LIFO,
166174
}
167175
}
168176
}

0 commit comments

Comments
 (0)