2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Nickel + Diesel (nickel_diesel) で `db_conn()` するとクラッシュするとき

Last updated at Posted at 2018-07-23

Rust初心者なので変なところで躓いてしまった。

要約

nickel_diesel crateをつかおうとして凡ミスに気づかず謎のクラッシュでハマったので気をつけようというお話。
RouterのHandler内でDatabase接続を使用するときには、Database接続を先にNickelに utilize しておこう。
そうでないと crash する。

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.14s
     Running `target/debug/myblog-rs`
Listening on http://127.0.0.1:8019
Ctrl-C to shutdown server
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:335:21
note: Run with `RUST_BACKTRACE=1` for a backtrace.

実装

extern crate とか use とかは省いて、概ねこんな感じ。
このコードの server.utilize(db_pool);server.utilize(router()); の行を逆に書いてしまうと、Databaseの接続が取得できずに (None が取れてしまって) ランタイムでクラッシュする。
具体的には、当該URLに対してHTTPリクエストを投げたときにクラッシュする。Backtrace見ると、index 関数の req.db_conn() している行で落ちる。

fn main() {
    let mut server: = Nickel::new();

    let mysql_url = "mysql://root:password@127.0.0.1:13306/blog";
    let connections: u32 = 5;
    let db_pool: DieselMiddleware<MysqlConnection> =
        DieselMiddleware::new(mysql_url, connections, Box::new(NopErrorHandler)).unwrap();

    server.utilize(db_pool);
    server.utilize(router());

    server.listen("127.0.0.1:8019").unwrap();
}

fn router() -> Router {
    let mut router: = Nickel::router();
    router.get("/", index);

    router
}

fn index<'mw>(req: &mut Request, res: Response<'mw>) -> MiddlewareResult<'mw> {
    let conn: PooledConnection<ConnectionManager<MysqlConnection>> = req.db_conn();
    let result = conn.execute("SELECT 1").unwrap();
    res.send(format!("{}", result))
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?