写了个带超时功能的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]>>)
评论区
写评论nice 虽然不懂!
我自己来回答吧,终于解决了,这样改即可,虽然还没看懂,代码如何下: