Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use actix-web 3 and rusoto 0.46 together?

When I try to use actix-web 3 and rusoto 0.46 together I get the following runtime error:

thread 'actix-rt:worker:0' panicked at 'there is no reactor running, must be called from the context of a Tokio 1.x runtime', /Users/matt/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.2.0/src/runtime/blocking/pool.rs:85:33

Small reproducible:

use actix_web::{get, App, HttpResponse, HttpServer, Responder}; // 3
use rusoto_core::Region; // 0.46 
use rusoto_dynamodb::{DynamoDb, DynamoDbClient, ListTablesInput};
use std::default::Default;

#[get("/tables")]
async fn tables(_req_body: String) -> impl Responder {
    let client = DynamoDbClient::new(Region::default());
    let list_tables_input: ListTablesInput = Default::default();
    match client.list_tables(list_tables_input).await {
        Ok(_output) => HttpResponse::Ok().finish(),
        Err(_error) => HttpResponse::InternalServerError().finish(),
    }
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().service(tables))
        .bind("0.0.0.0:8080")?
        .run()
        .await
}

Accompanying Cargo file:

[package]
name = "hello_actix_rusoto"
version = "0.1.0"
authors = ["Matt Roberts <[email protected]>"]
edition = "2018"

[dependencies]
rusoto_core = "0.46"
rusoto_dynamodb = "0.46"
actix-web = "3"

Here is a link to a very small GitHub repo with the full code and here is how to reproduce the error:

git clone [email protected]:mattroberts297/hello_actix_rusoto.git
cd hello_actix_rusoto
docker-compose up -d
AWS_REGION=local cargo run &
curl -v 'http://127.0.0.1:8080/tables'
like image 821
Matt Roberts Avatar asked Feb 09 '21 13:02

Matt Roberts


People also ask

Is actix Web still maintained?

Actix Web is part of an Ecosystem of Crates Though actix is still maintained, its usefulness as a general tool is diminishing as the futures and async/await ecosystem matures.

Does actix Web use Tokio?

Actix Web runs on Tokio, providing full 1 compatibility with its huge ecosystem of crates. Each of the server's workers uses a single-threaded runtime.

Is actix Web fast?

Actix Web is a powerful, pragmatic, and extremely fast web framework for Rust.

What is actix Web?

Actix Web is a small, fast and powerful asynchronous Rust web framework for building APIs and web applications. It relies internally on Tokio and the futures crate. Actix Web also provides a synchronous API, which can be seamlessly integrated with its asynchronous API.

How do I implement actix web middleware in rust?

As with many things in Rust, middleware for the Actix Web framework is rather more complex. It requires implementing Transform and Service traits to get started, and you might also need an extractor that implements FromRequest to conveniently retrieve any extra data, such as the user object in the JS example above.

What is actix web?

Actix Web | A powerful, pragmatic, and extremely fast web framework for Rust. Forget about stringly typed objects, from request to response, everything has types. Actix provides a lot of features out of box.

What is the difference between actix-service 2 and 3 middleware traits?

In this post I’m using the middleware traits from actix-service version 2, used by actix-web 4.0 which is in beta as I write this. The primary difference from actix-web 3 is that the Transform trait used to have an associated Transform::Request type, but it is now a type parameter on the trait instead: Transform<S, Req>.

What's the difference between actix-Web 3 and actix Web 3 transform traits?

The primary difference from actix-web 3 is that the Transform trait used to have an associated Transform::Request type, but it is now a type parameter on the trait instead: Transform<S, Req>. In actual use, this makes very little difference. So we need a Transform, a Service, and maybe an extractor, but how do they fit together?


1 Answers

rusoto v0.46 depends on tokio v1.0. actix-web v3 however, is still using tokio v0.2. The two versions are not backward compatible, hence the error message. To solve this, you can upgrade to a newer version of actix-web:

actix-web = "4.0.0-beta.8"

Or you can use the tokio-compat2 compatibility layer. This requires prefixing any incompatible .await call with .compat()

async fn index() -> impl Responder {
    let client = DynamoDbClient::new("[region]");
    let tables = client.list_tables(list_tables_input).compat().await;
    // ...
}

Or you could downgrade rusoto to an older version that uses tokio v0.2:

rusoto_core = "0.43"
like image 77
Ibraheem Ahmed Avatar answered Oct 27 '22 01:10

Ibraheem Ahmed