< 返回版块

rust菜鸟 发表于 2018-11-03 17:49

写了个带超时功能的http请求,但最后不知道如何写才能把正确值和异常值取出来,代码如下:

pub fn timeout_request(req: Request<Body>) -> Result<hyper::Chunk, String>{
    
    let client = Client::new();
    let fut = client.request(req)
        .and_then(|res| {
            res.into_body().concat2()
        }).map_err(|e| format!("{:?}", e));

    match Core::new(){
        Ok(mut core) => {
            let handle = core.handle();
            let timeout_wrap = tokio_core::reactor::Timeout::new(Duration::from_millis(170), &handle); 
            if !timeout_wrap.is_ok(){
                return Err(format!("{:?}", timeout_wrap.err()));
            }
            let timeout = timeout_wrap.unwrap()
                    .then(|_| -> Result<hyper::Chunk, String> {Err("our timeout".to_string())});
            let work = fut.select(timeout);
            match core.run(work){
                Ok(res) => {
                    //在此不知道如何取正确值
                    Err("OK".to_string())
                },
                Err(e) => {
                    //在此不知道如何取出错误值
                    Err("Err".to_string())
                }
            }
        },
        Err(e) => Err(format!("{:?}", e)),
    }
}

res 里有这些值: (hyper::Chunk, futures::SelectNext<futures::MapErr<futures::AndThen<hyper::client::ResponseFuture, futures::stream::Concat2hyper::Body, [closure@src\helper\http_utils.rs:59:19: 61:10]>, [closure@src\helper\http_utils.rs:61:20: 61:42]>, futures::Then<tokio_core::reactor::Timeout, std::result::Result<hyper::Chunk, std::string::String>, [closure@src\helper\http_utils.rs:76:40: 76:108]>>)

e里是这些值: (std::string::String, futures::SelectNext<futures::MapErr<futures::AndThen<hyper::client::ResponseFuture, futures::stream::Concat2hyper::Body, [closure@src\helper\http_utils.rs:59:19: 61:10]>, [closure@src\helper\http_utils.rs:61:20: 61:42]>, futures::Then<tokio_core::reactor::Timeout, std::result::Result<hyper::Chunk, std::string::String>, [closure@src\helper\http_utils.rs:76:40: 76:108]>>)

评论区

写评论
飘金 2018-11-05 17:24

nice 虽然不懂!

作者 rust菜鸟 2018-11-03 18:37

我自己来回答吧,终于解决了,这样改即可,虽然还没看懂,代码如何下:

pub fn timeout_request(req: Request<Body>, timeout:Duration) -> Result<hyper::Chunk, String>{
    
    let client = Client::new();
    let fut = client.request(req)
        .and_then(|res| {
            res.into_body().concat2()
        }).map_err(|e| format!("{:?}", e))
          .map(Ok);

    match Core::new(){
        Ok(mut core) => {
            let handle = core.handle();
            let timeout_wrap = tokio_core::reactor::Timeout::new(timeout, &handle); 
            if !timeout_wrap.is_ok(){
                return Err(format!("{:?}", timeout_wrap.err()));
            }
            let timeout = timeout_wrap.unwrap()
                    .then(|_| -> Result<hyper::Chunk, String> {Err("request timeout".to_string())})
                    .map(Err);   
                    
            let work = fut.select(timeout)
            .then(|result| {
                    match result{
                        Ok((Ok(data), _other_future)) => Ok(data),
                        Ok((Err(_timeout), _other_future)) => {
                            Err(format!("{:?}", _timeout))
                        },
                        Err((e, _other_future)) => Err(format!("{:?}", e)),
                    }
            });
            core.run(work)
        },
        Err(e) => Err(format!("{:?}", e)),
    }
}
1 共 2 条评论, 1 页