【问题标题】:How passing ScyllaDB connection in Rust Actix如何在 Rust Actix 中传递 ScyllaDB 连接
【发布时间】:2021-11-01 08:19:44
【问题描述】:

我在 Rust Actix 上实现 scylla-rust-driver 时遇到问题。我只想用 scyllaDb 创建简单的 CRUD。

首先我为应用数据创建结构

    struct AppState {
      scy_session: Session,
      app_name: String,
    }

接下来我创建简单函数

    #[get("/")] 
    async fn index(data: web::Data<AppState>) -> String {
       // I want to CRUD in this function with ScyllaDB
       let app_name = &data.app_name; // <- get app_name
       format!("Hello {}!", app_name) // <- response with app_name
    }

最后 main.rs 是

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let session: Session = SessionBuilder::new()
        .known_node("localhost:9042")
        .user("username", "password")
        .build()
        .await
        .expect("Some error message");

    HttpServer::new(|| {
        App::new()
            .data(AppState {
                scy_session: session,
                app_name: String::from("Actix-web"),
            })
            .service(index)
    })
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

但显示错误:

error[E0277]: the trait bound `Session: Clone` is not satisfied in `[closure@src/main.rs:27:21: 34:6]`
  --> src/main.rs:27:5
   |
27 |       HttpServer::new(|| {
   |  _____^^^^^^^^^^^^^^^_-
   | |     |
   | |     within `[closure@src/main.rs:27:21: 34:6]`, the trait `Clone` is not implemented for `Session`

如何在 Actix web 上实际实现 scylla-rust-db ? 非常感谢

【问题讨论】:

  • 根本没有测试,但是如果你在闭包中添加move关键字呢? HttpServer::new(move || {
  • 仍然是同样的错误 @yolenoyer 先生 - 无论如何谢谢``` #[actix_web::main] | ^^^^^^^^^^^^^^^^^^ 在[closure@src/main.rs:27:21: 34:10] 内,Clone 的特征没有为Session 实现... 27 | HttpServer::new(move||{ | _____________________- 28 | | App::new() 29 | | .data(AppState { 30 | | scy_session: session, ```

标签: rust actix-web scylla


【解决方案1】:

Session 并没有什么特别之处,见这里https://github.com/scylladb/scylla-rust-driver/blob/main/examples/parallel-prepared.rs

解决方案是将其包装在Arc 中。我们不需要自己这样做,因为web::Data 自己会这样做。这是我得到的。

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

use scylla::{Session, SessionBuilder};

struct AppState {
    scy_session: Session,
    app_name: String,
}

/// Use the `Data<T>` extractor to access data in a handler.
async fn index(data: web::Data<AppState>) -> impl Responder {
    let app_name = &data.app_name; // <- get app_name
    HttpResponse::Ok().body(format!("Hello {}!", app_name))
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let session: Session = SessionBuilder::new()
        .known_node("localhost:9042")
        .user("username", "passwor")
        .build()
        .await
        .expect("Some error message");
    let data = web::Data::new(AppState {
        scy_session: session,
        app_name: String::from("Actix-web"),
    });

    HttpServer::new(move || {
        App::new()
            .data(data.clone())
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}

另见:https://stackoverflow.com/a/67772754/1418750

【讨论】:

  • 哇——它的作品。但现在显示错误:线程“主”在“没有反应器运行,必须从 Tokio 1.x 运行时的上下文中调用”时惊慌失措,/Users/hhh/.cargo/registry/src/github.com-1ecc6299db9ec823 /tokio-1.13.0/src/runtime/context.rs:37:26 注意:使用RUST_BACKTRACE=1 环境变量运行以在仲裁线程中显示回溯恐慌。
  • 可能是因为actix版本,你没有说明你使用的是哪个。
  • 我正在使用这个 actix-web="3" actix-rt="1.1.1" scylla = "0.2.1" tokio="0.2.25"
  • 是的,正确,在我更改版本后一切正常 actix-web = "4.0.0-beta.5" tokio = { version = "1.4.0", features = ["time"] }非常感谢
  • 我很好奇为什么 Session 不需要包裹在 Mutex 中。没有它似乎工作正常。
猜你喜欢
  • 1970-01-01
  • 2020-10-27
  • 2019-12-13
  • 2019-09-16
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-14
相关资源
最近更新 更多