4

我正在使用 Rust 2018 Stable 和 Actix-Web 编写 Web 服务。使用 Reqwest,我正在从一个路由处理程序函数中向不同站点发出 HTTP 请求。简单地说它看起来像这样

extern crate reqwest;
use actix_web;
use reqwest::Url;

pub fn testing(req: actix_web::HttpRequest) -> actix_web::Result<actix_web::HttpResponse> {
    println!(">>> testing request begin");
    let url = Url::parse("https://example.com/").unwrap();
    println!(">>> testing url built");
    let req = reqwest::Client::new().post(url);
    println!(">>> testing req prepared");
    let res_struct = req.send();
    println!(">>> testing res_struct received");
    let res = res_struct.unwrap();
    println!(">>> testing res unwrapped");
    Ok(format!("done.").into())
}

这不起作用,我收到以下错误消息(错误打印了 8 次,“worker:1”到“worker:8”,尽管只调用了一次函数):

thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()` 
on an `Err` value: Error(BlockingClientInFutureContext, 
"https://www.example.com/")', src/libcore/result.rs:999:5
Panic in Arbiter thread, shutting down system.

谷歌在"BlockingClientInFutureContext"上没有发现任何有用的东西,但我猜它与 async/await 或 Tokio 自己的未来有某种关系?

感谢您提供有关阅读内容的任何指示。另外,我是 Rust 的新手。

从 Actix-Web HTttpServer 调用处理函数:

HttpServer::new(|| App::new().service(
    web::resource("/testing").route(
        web::get().to(views::testing)
    )
)).bind("127.0.0.1:8001")?.run()
4

4 回答 4

5

我有一个类似的问题。我的解决方案是在您的货物文件中将 Reqwest crate 版本锁定为 0.9.17,然后重新构建。

reqwest = "=0.9.17"

除非您在两者上都使用异步功能,否则较新版本的 Reqwest 似乎已被 Actix-web 破坏。供参考:https ://github.com/seanmonstar/reqwest/issues/541

于 2019-08-20T21:18:18.703 回答
3

事实证明,actix_web::web::block()这是正确的猜测。使用它可以进行阻塞调用。block()返回一个 Future,一旦网络请求返回数据,它就会解析。非常接近 JS 中的 Promises,加上.from_err()里面的。

pub fn testing(_req: actix_web::HttpRequest)
    -> impl Future<Item = HttpResponse, Error = Error>
{
    println!(">>> testing request begin");
    let url = Url::parse("https://example.com/").unwrap();
    println!(">>> testing url built");
    let req = reqwest::Client::new().get(url);
    println!(">>> testing req prepared");

    actix_web::web::block(move || {
        println!(">>> testing res received");
        req.send()
    })
    .from_err()
    .and_then(|res| {
        println!(">>> testing res: {:?}", &res);
        HttpResponse::Ok().content_type("text/html").body("Hello!")
    })
}

此外,在main.rs路由中必须使用.to_async()而不是简单地调用.to()

HttpServer::new(|| App::new().service(
    web::resource("/testing").route(
        web::get().to_async(views::testing)
    )
)).bind("127.0.0.1:8001")?.run()
于 2019-08-19T21:07:33.673 回答
1

TL;DR:升级到 reqwest 0.9.22 或更高版本。

该错误表明您正在尝试从异步上下文中进行阻塞网络调用(Actix Web 处理程序被异步调用)。这在 reqwest 版本 0.9.17 - 0.9.21 中不受支持。

自 0.9.22 起,作者已删除此错误以支持警告。了解更多信息:

https://github.com/seanmonstar/reqwest/pull/670

https://github.com/seanmonstar/reqwest/issues/541

于 2019-10-10T05:30:22.530 回答
0

调用unwrap()失败,因为返回了错误。最好unwrap()在生产代码中避免,因为这通常意味着我们试图查看“期望”值(通常称为“快乐路径”)而忽略错误路径。

此代码有效:

use actix_web;
use reqwest::Url;

fn main()  {
    println!(">>> testing request begin");
    let url = Url::parse("http:/example.com/").unwrap();
    println!(">>> testing url built");
    let req = reqwest::Client::new().post(url);
    println!(">>> testing req prepared");
    let res_struct = req.send();
    println!(">>> testing res_struct received");
    match res_struct {
        Ok(r)=> println!("response: {:?}", r),
        Err(e)=> println!("error: {}", e),
    }
//    let res = res_struct.unwrap();
    println!("done.");
}

输出是:

Finished dev [unoptimized + debuginfo] target(s) in 2.63s
     Running `target/debug/untitled`
>>> testing request begin
>>> testing url built
>>> testing req prepared
>>> testing res_struct received
error: http://example.com/: error trying to connect: failed to lookup address information: nodename nor servname provided, or not known
>>> testing res unwrapped
done.

上面的代码没有恐慌,但服务器example.com没有提供良好的响应。如果我使用有效的 URL 重新运行它,例如https://cisco.com,我不会收到任何错误:

>>> testing request begin
>>> testing url built
>>> testing req prepared
>>> testing res_struct received
response: Response { url: "https://www.cisco.com/", status: 200, headers: {"server": "Apache", "etag": "\"1732e-59058880c8465\"", "accept-ranges": "bytes", "strict-transport-security": "max-age=31536000", "cdchost": "wemxweb-publish-prod2-02", "x-xss-protection": "1; mode=block", "x-test-debug": "nURL=www.cisco.com,realm=0,isRealm=0,realmDomain=0,shortrealm=0", "content-security-policy": "upgrade-insecure-requests; frame-ancestors *.cisco.com *.jasper.com *.ciscospark.com *.ciscolive.com  http://cisco.lookbookhq.com https://cisco.lookbookhq.com testcisco.marketing.adobe.com cisco.marketing.adobe.com ciscosales.my.salesforce.com test.salesforce.com zedo.com hindustantimes.com economictimes.indiatimes.com *.webex.com *.cdw.com *.cdwg.com *.cdw.ca *.meraki-go.com http://ciscopartners.lookbookhq.com https://ciscopartners.lookbookhq.com ciscolearningsystem.com ciscocustomer.lookbookhq.com cisco.lookbookhq.com;", "content-type": "text/html", "expires": "Sun, 18 Aug 2019 12:10:23 GMT", "cache-control": "max-age=0, no-cache, no-store", "pragma": "no-cache", "date": "Sun, 18 Aug 2019 12:10:23 GMT", "connection": "keep-alive", "vary": "Accept-Encoding"} }
>>> testing res unwrapped
done.
于 2019-08-18T12:13:12.083 回答