-
-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit refactors the support for connection pools for diesel. Instead of only allowing `r2d2` based pools we now abstract the actual pool away. It also adds support for `deadpool-diesel` based pools as alternative.
- Loading branch information
Showing
5 changed files
with
312 additions
and
122 deletions.
There are no files selected for viewing
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,69 @@ | ||
use std::net::SocketAddr; | ||
|
||
use axum::{ | ||
error_handling::HandleErrorLayer, response::IntoResponse, routing::get, BoxError, Router, | ||
}; | ||
use deadpool_diesel::{Manager, Pool}; | ||
use diesel::prelude::*; | ||
use http::StatusCode; | ||
use serde::{Deserialize, Serialize}; | ||
use time::Duration; | ||
use tower::ServiceBuilder; | ||
use tower_sessions::{ | ||
diesel_store::DieselStore, session_store::ExpiredDeletion, Session, SessionManagerLayer, | ||
}; | ||
|
||
const COUNTER_KEY: &str = "counter"; | ||
|
||
#[derive(Serialize, Deserialize, Default)] | ||
struct Counter(usize); | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let manager = Manager::<SqliteConnection>::new(":memory:", deadpool_diesel::Runtime::Tokio1); | ||
let pool = Pool::builder(manager).max_size(1).build()?; | ||
let session_store = DieselStore::new(pool); | ||
session_store.migrate().await?; | ||
|
||
let deletion_task = tokio::task::spawn( | ||
session_store | ||
.clone() | ||
.continuously_delete_expired(tokio::time::Duration::from_secs(60)), | ||
); | ||
|
||
let session_service = ServiceBuilder::new() | ||
.layer(HandleErrorLayer::new(|_: BoxError| async { | ||
StatusCode::BAD_REQUEST | ||
})) | ||
.layer( | ||
SessionManagerLayer::new(session_store) | ||
.with_secure(false) | ||
.with_max_age(Duration::seconds(10)), | ||
); | ||
|
||
let app = Router::new() | ||
.route("/", get(handler)) | ||
.layer(session_service); | ||
|
||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); | ||
axum::Server::bind(&addr) | ||
.serve(app.into_make_service()) | ||
.await?; | ||
|
||
deletion_task.await??; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn handler(session: Session) -> impl IntoResponse { | ||
let counter: Counter = session | ||
.get(COUNTER_KEY) | ||
.expect("Could not deserialize.") | ||
.unwrap_or_default(); | ||
|
||
session | ||
.insert(COUNTER_KEY, counter.0 + 1) | ||
.expect("Could not serialize."); | ||
|
||
format!("Current count: {}", counter.0) | ||
} |
Oops, something went wrong.