-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add deadpool support and refactor r2d2 support for sharing.
- Loading branch information
1 parent
c75ac92
commit fca9bf5
Showing
13 changed files
with
149 additions
and
120 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//! Deadpool support for Butane. | ||
use super::ConnectionManager; | ||
use crate::db::{BackendConnectionAsync, ConnectionAsync}; | ||
use crate::Result; | ||
use deadpool::managed::{Manager, Metrics, RecycleError, RecycleResult}; | ||
|
||
impl Manager for ConnectionManager { | ||
type Type = ConnectionAsync; | ||
type Error = crate::Error; | ||
|
||
async fn create(&self) -> Result<ConnectionAsync> { | ||
crate::db::connect_async(&self.spec).await | ||
} | ||
|
||
async fn recycle(&self, conn: &mut ConnectionAsync, _: &Metrics) -> RecycleResult<Self::Error> { | ||
if conn.is_closed() { | ||
return Err(RecycleError::message("Connection is closed")); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//! Types, traits, and methods for interacting with a database. | ||
//! | ||
//! The different ways of referring to a database handle may present | ||
//! some initial confusion. | ||
//! * `ConnectionMethods` is a trait containing the methods available on a database connection or a transaction. | ||
//! Most methods on a [DataObject][crate::DataObject] or a [Query][crate::query::Query] require an | ||
//! implementation of `ConnectionMethods`. | ||
//! * `BackendConnection` is a trait representing a direct connection to a database backend. It is a superset | ||
//! of `ConnectionMethods` and also includes the ability to create a transaction. | ||
//! * `Transaction` is a struct representing a database transaction. It implements `ConnectionMethods`. | ||
//! * `Connection` is a convenience struct containing a boxed `BackendConnection`. It cannot do anything other than | ||
//! what a `BackendConnection` can do, but allows using a single concrete type that is not tied to a particular | ||
//! database backend. It is returned by the `connect` method. | ||
pub use butane_core::db::*; | ||
|
||
#[cfg(feature = "r2d2")] | ||
mod r2; | ||
|
||
#[cfg(feature = "deadpool")] | ||
mod deadpool; | ||
|
||
/// Connection manager used with connection pooling systems such as r2d2 or deadpool. | ||
/// With the `r2d2` feature enabled, it implements `r2d2::ManageConnection`. | ||
/// With the `deadpool` feature enabled, it implements `deadpool::managed::Manager`. | ||
#[cfg(any(feature = "deadpool", feature = "r2d2"))] | ||
#[derive(Clone, Debug)] | ||
pub struct ConnectionManager { | ||
spec: ConnectionSpec, | ||
} | ||
#[cfg(any(feature = "deadpool", feature = "r2d2"))] | ||
impl ConnectionManager { | ||
/// Create a new ConnectionManager from a [ConnectionSpec]. | ||
pub fn new(spec: ConnectionSpec) -> Self { | ||
ConnectionManager { spec } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
//! R2D2 support for Butane. | ||
use r2d2::ManageConnection; | ||
|
||
use super::ConnectionManager; | ||
use crate::db::{BackendConnection, Connection}; | ||
use crate::Result; | ||
|
||
impl ManageConnection for ConnectionManager { | ||
type Connection = Connection; | ||
type Error = crate::Error; | ||
|
||
fn connect(&self) -> Result<Connection> { | ||
crate::db::connect(&self.spec) | ||
} | ||
|
||
fn is_valid(&self, conn: &mut Connection) -> Result<()> { | ||
conn.execute("SELECT 1") | ||
} | ||
|
||
fn has_broken(&self, conn: &mut Connection) -> bool { | ||
conn.is_closed() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.