This is an automated email from the ASF dual-hosted git repository.
hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-rust-diesel-adapter.git
The following commit(s) were added to refs/heads/master by this push:
new 892c1df feat: expose ConnectionPool and re-export diesel for with_pool
892c1df is described below
commit 892c1dfe1b4e0b76fcec99a4b0b58e9e104b25be
Author: Yang Luo <[email protected]>
AuthorDate: Thu Aug 6 23:25:35 2026 +0800
feat: expose ConnectionPool and re-export diesel for with_pool
---
README.md | 34 ++++++++++++++++++++++++++++++++++
src/adapter.rs | 29 +++++++++++++++++++++++++++--
src/lib.rs | 4 ++--
3 files changed, 63 insertions(+), 4 deletions(-)
diff --git a/README.md b/README.md
index c30aef9..139cc16 100644
--- a/README.md
+++ b/README.md
@@ -68,3 +68,37 @@ async fn main() -> Result<()> {
Ok(())
}
```
+
+### Sharing an existing connection pool
+
+`DieselAdapter::new` creates its own `r2d2` pool. If your application already
has
+one, use `DieselAdapter::with_pool` instead so Casbin reuses your connections
+rather than opening a second pool:
+
+```rust
+use diesel_adapter::casbin::prelude::*;
+use diesel_adapter::diesel::r2d2::{ConnectionManager, Pool};
+use diesel_adapter::{ConnectionPool, DieselAdapter};
+
+#[tokio::main]
+async fn main() -> Result<()> {
+ let m = DefaultModel::from_file("examples/rbac_model.conf").await?;
+
+ // your application's pool
+ let pool: ConnectionPool = Pool::builder()
+ .max_size(8)
+ .build(ConnectionManager::new(
+ "postgres://casbin_rs:[email protected]:5432/casbin",
+ ))
+ .unwrap();
+
+ let a = DieselAdapter::with_pool(pool.clone())?;
+ let mut e = Enforcer::new(m, a).await?;
+ Ok(())
+}
+```
+
+`ConnectionPool` is an alias for `Pool<ConnectionManager<Connection>>`, where
+`Connection` resolves to `PgConnection`, `MysqlConnection` or
`SqliteConnection`
+depending on the enabled feature. `diesel` is re-exported as
+`diesel_adapter::diesel`, so you do not need to match diesel versions by hand.
diff --git a/src/adapter.rs b/src/adapter.rs
index 5cbb3af..2417841 100644
--- a/src/adapter.rs
+++ b/src/adapter.rs
@@ -15,8 +15,15 @@ use tokio::task::spawn_blocking;
use std::time::Duration;
+/// The concrete `diesel` connection type used by this adapter, decided by the
+/// enabled database feature (`postgres`, `mysql` or `sqlite`).
+pub type Connection = adapter::Connection;
+
+/// The `r2d2` connection pool type accepted by [`DieselAdapter::with_pool`].
+pub type ConnectionPool = Pool<ConnectionManager<Connection>>;
+
pub struct DieselAdapter {
- pool: Pool<ConnectionManager<adapter::Connection>>,
+ pool: ConnectionPool,
is_filtered: bool,
}
@@ -34,7 +41,25 @@ impl DieselAdapter {
Self::with_pool(pool)
}
- pub fn with_pool(pool: Pool<ConnectionManager<adapter::Connection>>) ->
Result<Self> {
+ /// Build an adapter on top of an already existing connection pool, so the
+ /// pool can be shared with the rest of the application instead of letting
+ /// [`DieselAdapter::new`] open a second one.
+ ///
+ /// ```no_run
+ /// use diesel_adapter::diesel::r2d2::{ConnectionManager, Pool};
+ /// use diesel_adapter::{ConnectionPool, DieselAdapter};
+ ///
+ /// # fn main() -> diesel_adapter::casbin::Result<()> {
+ /// let pool: ConnectionPool = Pool::builder()
+ /// .max_size(8)
+ ///
.build(ConnectionManager::new("postgres://casbin_rs:[email protected]:5432/casbin"))
+ /// .unwrap();
+ ///
+ /// let adapter = DieselAdapter::with_pool(pool.clone())?;
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub fn with_pool(pool: ConnectionPool) -> Result<Self> {
let conn = pool
.get()
.map_err(|err|
CasbinError::from(AdapterError(Box::new(Error::PoolError(err)))));
diff --git a/src/lib.rs b/src/lib.rs
index 915a8fc..f342b6f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
#[macro_use]
-extern crate diesel;
+pub extern crate diesel;
mod adapter;
mod error;
@@ -11,5 +11,5 @@ mod actions;
pub use casbin;
-pub use adapter::DieselAdapter;
+pub use adapter::{Connection, ConnectionPool, DieselAdapter};
pub use error::Error;
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]