如何解决Actix-Web中的流超时问题

时间:2019-02-09 18:28:31

标签: rust streaming rust-actix

我想将Actix-web用作简单的代理服务器,但是当我在流模式下使用大文件时,该服务器发生超时错误,因此只有一小部分文件将被下载。

我真的很困惑,因为它是来自actix-web示例的示例代码!

示例代码:

extern crate actix;
extern crate actix_web;
extern crate env_logger;
extern crate futures;

use actix_web::{
    client, middleware, server, App, AsyncResponder, Body, Error, HttpMessage,
    HttpRequest, HttpResponse,
};
use futures::{Future, Stream};

/// Stream client request response and then send body to a server response
fn index(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
    client::ClientRequest::get("http://127.0.0.1:8081/")
        .finish().unwrap()
        .send()
        .map_err(Error::from)          // <- convert SendRequestError to an Error
        .and_then(
            |resp| resp.body()         // <- this is MessageBody type, resolves to complete body
                .from_err()            // <- convert PayloadError to an Error
                .and_then(|body| {     // <- we got complete body, now send as server response
                    Ok(HttpResponse::Ok().body(body))
                }))
        .responder()
}

/// streaming client request to a streaming server response
fn streaming(_req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
    // send client request
    client::ClientRequest::get("https://gemmei.ftp.acc.umu.se/debian-cd/current/amd64/iso-cd/debian-9.7.0-amd64-netinst.iso")
        .finish().unwrap()
        .send()                         // <- connect to host and send request
        .map_err(Error::from)           // <- convert SendRequestError to an Error
        .and_then(|resp| {              // <- we received client response
            Ok(HttpResponse::Ok()
               // read one chunk from client response and send this chunk to a server response
               // .from_err() converts PayloadError to an Error
               .body(Body::Streaming(Box::new(resp.payload().from_err()))))
        })
        .responder()
}

fn main() {
    ::std::env::set_var("RUST_LOG", "actix_web=info");
    env_logger::init();
    let sys = actix::System::new("http-proxy");

    server::new(|| {
        App::new()
            .middleware(middleware::Logger::default())
            .resource("/streaming", |r| r.f(streaming))
            .resource("/", |r| r.f(index))
    }).workers(1)
        .bind("127.0.0.1:8080")
        .unwrap()
        .start();

    println!("Started http server: 127.0.0.1:8080");
    let _ = sys.run();
}

依赖项:

[dependencies]
env_logger = "0.5"
futures = "0.1"
actix = "0.7"
actix-web = { version="0.7", features=["ssl"] }

日志出现错误

ERROR 2019-02-09T18:10:16Z: actix_web::pipeline: Error occurred during request handling: Timeout while waiting for response
ERROR 2019-02-09T18:10:16Z: actix_web::server::h1: Unhandled error1: Timeout while waiting for response

如您在这张照片中看到的,它的数据没有内容长度和文件名。 Sample Picture

1 个答案:

答案 0 :(得分:0)

您应该串流正文:使用.streaming代替.body

use actix_web::client::Client;
use actix_web::{web, App, Error, HttpResponse, HttpServer};
use futures::future::Future;

fn main() {
    HttpServer::new(|| {
        App::new()
            .data(Client::new())
            .route("/", web::to_async(handler))
    })
    .bind("127.0.0.1:8000")
    .expect("Cannot bind to port 8000")
    .run()
    .expect("Unable to run server");
}

fn handler(client: web::Data<Client>) -> impl Future<Item = HttpResponse, Error = Error> {
    client.get("https://gensho.ftp.acc.umu.se/debian-cd/current/amd64/iso-cd/debian-10.0.0-amd64-netinst.iso")
        .send()
        .map_err(Error::from)
        .and_then(|res| {
            HttpResponse::build(res.status()).streaming(res)
        })
}

对于代理,请注意,the full proxy example已更新为修复issues with the content length header