From 98b062e9b564f399fb06da935ea6d6e37fa1d3f5 Mon Sep 17 00:00:00 2001 From: Zain Kabani Date: Wed, 24 May 2023 11:59:31 -0400 Subject: [PATCH 1/5] Add LIFO option for getting resources in the pool --- bb8/src/api.rs | 20 ++++++++++++++++++++ bb8/src/internals.rs | 14 +++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index f0d1b82..6dc8219 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -74,6 +74,14 @@ impl Pool { } } +#[derive(Debug)] +pub enum QueueStrategy { + /// Last in first out + LIFO, + /// First in first out + FIFO, +} + /// A builder for a connection pool. #[derive(Debug)] pub struct Builder { @@ -95,6 +103,8 @@ pub struct Builder { pub(crate) error_sink: Box>, /// The time interval used to wake up and reap connections. pub(crate) reaper_rate: Duration, + /// Queue strategy (FIFO or LIFO) + pub(crate) queue_strategy: QueueStrategy, /// User-supplied trait object responsible for initializing connections pub(crate) connection_customizer: Option>>, _p: PhantomData, @@ -112,6 +122,7 @@ impl Default for Builder { retry_connection: true, error_sink: Box::new(NopErrorSink), reaper_rate: Duration::from_secs(30), + queue_strategy: QueueStrategy::FIFO, connection_customizer: None, _p: PhantomData, } @@ -260,6 +271,15 @@ impl Builder { self } + /// Sets the queue strategy to be used by the pool + /// + /// Defaults to `LIFO`. + #[must_use] + pub fn queue_strategy(mut self, queue_strategy: QueueStrategy) -> Self { + self.queue_strategy = queue_strategy; + self + } + /// Set the connection customizer to customize newly checked out connections #[must_use] pub fn connection_customizer( diff --git a/bb8/src/internals.rs b/bb8/src/internals.rs index cffd928..0050a67 100644 --- a/bb8/src/internals.rs +++ b/bb8/src/internals.rs @@ -2,7 +2,7 @@ use std::cmp::min; use std::sync::Arc; use std::time::Instant; -use crate::lock::Mutex; +use crate::{lock::Mutex, api::QueueStrategy}; use futures_channel::oneshot; use crate::api::{Builder, ManageConnection}; @@ -42,6 +42,7 @@ where conns: VecDeque>, num_conns: u32, pending_conns: u32, + queue_strategy: QueueStrategy, } impl PoolInternals @@ -80,8 +81,14 @@ where } // Queue it in the idle queue - self.conns - .push_back(IdleConn::from(guard.conn.take().unwrap())); + match self.queue_strategy { + QueueStrategy::FIFO => self + .conns + .push_back(IdleConn::from(guard.conn.take().unwrap())), + QueueStrategy::LIFO => self + .conns + .push_front(IdleConn::from(guard.conn.take().unwrap())), + } } pub(crate) fn connect_failed(&mut self, _: Approval) { @@ -163,6 +170,7 @@ where conns: VecDeque::new(), num_conns: 0, pending_conns: 0, + queue_strategy: QueueStrategy::LIFO, } } } From 178012242aa6160d8a6589fdc7a00159987ac1aa Mon Sep 17 00:00:00 2001 From: Zain Kabani Date: Wed, 24 May 2023 13:40:34 -0400 Subject: [PATCH 2/5] Fix comment and fmt --- bb8/src/api.rs | 2 +- bb8/src/internals.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index 6dc8219..e1a72fe 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -273,7 +273,7 @@ impl Builder { /// Sets the queue strategy to be used by the pool /// - /// Defaults to `LIFO`. + /// Defaults to `FIFO`. #[must_use] pub fn queue_strategy(mut self, queue_strategy: QueueStrategy) -> Self { self.queue_strategy = queue_strategy; diff --git a/bb8/src/internals.rs b/bb8/src/internals.rs index 0050a67..1ec3e42 100644 --- a/bb8/src/internals.rs +++ b/bb8/src/internals.rs @@ -2,7 +2,7 @@ use std::cmp::min; use std::sync::Arc; use std::time::Instant; -use crate::{lock::Mutex, api::QueueStrategy}; +use crate::{api::QueueStrategy, lock::Mutex}; use futures_channel::oneshot; use crate::api::{Builder, ManageConnection}; From 7db3e447d2ea2ded44e06d4e8fe08fcf45b0ea5b Mon Sep 17 00:00:00 2001 From: Zain Kabani Date: Wed, 24 May 2023 16:11:37 -0400 Subject: [PATCH 3/5] Fix linting errors --- bb8/src/api.rs | 8 ++++---- bb8/src/internals.rs | 11 ++++------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index e1a72fe..757c934 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -77,9 +77,9 @@ impl Pool { #[derive(Debug)] pub enum QueueStrategy { /// Last in first out - LIFO, + Lifo, /// First in first out - FIFO, + Fifo, } /// A builder for a connection pool. @@ -122,7 +122,7 @@ impl Default for Builder { retry_connection: true, error_sink: Box::new(NopErrorSink), reaper_rate: Duration::from_secs(30), - queue_strategy: QueueStrategy::FIFO, + queue_strategy: QueueStrategy::Fifo, connection_customizer: None, _p: PhantomData, } @@ -273,7 +273,7 @@ impl Builder { /// Sets the queue strategy to be used by the pool /// - /// Defaults to `FIFO`. + /// Defaults to `Fifo`. #[must_use] pub fn queue_strategy(mut self, queue_strategy: QueueStrategy) -> Self { self.queue_strategy = queue_strategy; diff --git a/bb8/src/internals.rs b/bb8/src/internals.rs index 1ec3e42..a7a33bb 100644 --- a/bb8/src/internals.rs +++ b/bb8/src/internals.rs @@ -81,13 +81,10 @@ where } // Queue it in the idle queue + let conn = IdleConn::from(guard.conn.take().unwrap()); match self.queue_strategy { - QueueStrategy::FIFO => self - .conns - .push_back(IdleConn::from(guard.conn.take().unwrap())), - QueueStrategy::LIFO => self - .conns - .push_front(IdleConn::from(guard.conn.take().unwrap())), + QueueStrategy::Fifo => self.conns.push_back(conn), + QueueStrategy::Lifo => self.conns.push_front(conn), } } @@ -170,7 +167,7 @@ where conns: VecDeque::new(), num_conns: 0, pending_conns: 0, - queue_strategy: QueueStrategy::LIFO, + queue_strategy: QueueStrategy::Lifo, } } } From bec25ee96aa395ef267f3396e58f1ce4387af819 Mon Sep 17 00:00:00 2001 From: Zain Kabani Date: Wed, 24 May 2023 16:23:51 -0400 Subject: [PATCH 4/5] update comments on enum and use default trait --- bb8/src/api.rs | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index 757c934..599b7e2 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -74,14 +74,6 @@ impl Pool { } } -#[derive(Debug)] -pub enum QueueStrategy { - /// Last in first out - Lifo, - /// First in first out - Fifo, -} - /// A builder for a connection pool. #[derive(Debug)] pub struct Builder { @@ -110,6 +102,19 @@ pub struct Builder { _p: PhantomData, } +#[derive(Debug, Default)] +pub enum QueueStrategy { + #[default] + /// First in first out + /// This strategy behaves like a queue + /// It will evenly spread load on all existing connections, resetting their idle timeouts, maintaining the pool size + Fifo, + /// Last in first out + /// This behaves like a stack + /// It will use the most recently used connection and help to keep the total pool size small by evicting idle connections + Lifo, +} + impl Default for Builder { fn default() -> Self { Builder { @@ -122,7 +127,7 @@ impl Default for Builder { retry_connection: true, error_sink: Box::new(NopErrorSink), reaper_rate: Duration::from_secs(30), - queue_strategy: QueueStrategy::Fifo, + queue_strategy: QueueStrategy::default(), connection_customizer: None, _p: PhantomData, } From 77516fa40ee1eebd6a63433b0c45fac35218a0d4 Mon Sep 17 00:00:00 2001 From: Zain Kabani Date: Wed, 24 May 2023 17:00:12 -0400 Subject: [PATCH 5/5] Implement default for queue strategy --- bb8/src/api.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bb8/src/api.rs b/bb8/src/api.rs index 599b7e2..357d052 100644 --- a/bb8/src/api.rs +++ b/bb8/src/api.rs @@ -102,9 +102,8 @@ pub struct Builder { _p: PhantomData, } -#[derive(Debug, Default)] +#[derive(Debug)] pub enum QueueStrategy { - #[default] /// First in first out /// This strategy behaves like a queue /// It will evenly spread load on all existing connections, resetting their idle timeouts, maintaining the pool size @@ -115,6 +114,12 @@ pub enum QueueStrategy { Lifo, } +impl Default for QueueStrategy { + fn default() -> Self { + QueueStrategy::Fifo + } +} + impl Default for Builder { fn default() -> Self { Builder {