so-*_*ude 2 rust rust-warp rust-sqlx
一个 Rust 新手,尝试通过结合来编写一个 Web 服务
https://github.com/seanmonstar/warp/blob/master/examples/todos.rs和https://github.com/launchbadge/sqlx/blob/master/examples/postgres/todos/src/main.rs
以下代码处于运行状态。我的问题是,我是否需要为每个处理程序克隆 dbpool?Rust 中的惯用方式是什么(我来自 Java/Kotlin->Go 背景,FWIW)
#![deny(warnings)]
use sqlx::postgres::{PgPoolOptions};
use std::env;
use warp::Filter;
#[tokio::main]
async fn main() -> Result<(), sqlx::Error> {
let pool = PgPoolOptions::new()
.max_connections(5)
.connect("postgres://:@localhost/todo_db").await?;
if env::var_os("RUST_LOG").is_none() {
env::set_var("RUST_LOG", "todos=info");
}
pretty_env_logger::init();
let api = filters::todos(pool);
let routes = api.with(warp::log("todos"));
// Start up the server...
warp::serve(routes).run(([127, 0, 0, 1], 3030)).await;
Ok(())
}
mod filters {
use sqlx::{Pool, Postgres};
use super::handlers;
use super::models::{ListOptions, Todo};
use warp::Filter;
pub fn todos(
db: Pool<Postgres>,
) -> impl Filter<Extract=impl warp::Reply, Error=warp::Rejection> + Clone {
todos_list(db)
}
/// GET /todos?offset=3&limit=5
pub fn todos_list(
db: Pool<Postgres>,
) -> impl Filter<Extract=impl warp::Reply, Error=warp::Rejection> + Clone {
warp::path!("todos")
.and(warp::get())
.and(warp::query::<ListOptions>())
.and(with_db(db))
.and_then(handlers::list_todos)
}
fn with_db(db: Pool<Postgres>) -> impl Filter<Extract=(Pool<Postgres>, ), Error=std::convert::Infallible> + Clone {
warp::any().map(move || db.clone())
}
fn _json_body() -> impl Filter<Extract=(Todo, ), Error=warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
}
mod handlers {
use super::models::{ListOptions};
use std::convert::Infallible;
use sqlx::{Pool, Postgres};
use crate::models::Todo;
pub async fn list_todos(_opts: ListOptions, db: Pool<Postgres>) -> Result<impl warp::Reply, Infallible> {
let recs = sqlx::query!(
r#"
SELECT id, description, done
FROM todos
ORDER BY id
"#
)
.fetch_all(&db).await.expect("Some error message");
let x: Vec<Todo> = recs.iter().map(|rec| {
Todo { id: rec.id, text: rec.description.clone(), completed: rec.done }
}).collect();
Ok(warp::reply::json(&x))
}
}
mod models {
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
pub struct Todo {
pub id: i64,
pub text: String,
pub completed: bool,
}
// The query parameters for list_todos.
#[derive(Debug, Deserialize)]
pub struct ListOptions {
pub offset: Option<usize>,
pub limit: Option<usize>,
}
}
Run Code Online (Sandbox Code Playgroud)
正如@cdhowie 指出的那样,虽然copying 池只会增加 an 中的引用计数器Arc并且相对便宜,但如果您愿意这样做,则可以避免它:.fetch_all(db)只需要一个不可变的引用。因此,您可以传入一个&'static Pool<\xe2\x80\xa6>. 一个棘手的事情是:你不能直接声明一个
static POOL: Pool<Postgres> = \xe2\x80\xa6;\nRun Code Online (Sandbox Code Playgroud)\n因为你没有什么可以为\xe2\x80\xa6. 只能const fn在初始化statics时使用,不能使用.await.
相反,您可以使用OnceCell. 存在多种变体,其中包含的变体tokio可能是最方便的:
static POOL: OnceCell<Pool<Postgres>> = OnceCell::const_new();\n#[tokio::main]\nasync fn main() -> Result<(), sqlx::Error> {\n POOL.get_or_try_init(|| async {\n PgPoolOptions::new()\n .max_connections(5)\n .connect("postgres://:@localhost/todo_db")\n .await\n })\n .await?;\n // Later, just access your pool with POOL.get().unwrap()\n // You don't need the with_db filter anymore\nRun Code Online (Sandbox Code Playgroud)\n但就我个人而言,我更喜欢创建与应用程序本身一样长的连接或池Box::leak(Box::new(PgPoolOptions()\xe2\x80\xa6.await?))。如果您认为这很糟糕,因为它(显然\xe2\x80\xa6)泄漏了内存,请考虑这一点:OnceCell永远不会被删除或释放。这也意味着既不允许OnceCell也Box::leak不允许完全关闭连接池,而Arc理论上您的内部代码可以这样做。
| 归档时间: |
|
| 查看次数: |
1659 次 |
| 最近记录: |