Rust 在Iron and hyper中重用hyper::client和tokio_内核
我在熨斗处理器内发出客户请求。如何重用Tokio的Rust 在Iron and hyper中重用hyper::client和tokio_内核,rust,hyper,rust-tokio,Rust,Hyper,Rust Tokio,我在熨斗处理器内发出客户请求。如何重用Tokio的核心和Hyper的客户端?我用的是hyper 0.11.0和tokio core 0.1 fn get_result(req: &mut Request) -> IronResult<Response> { let mut payload = String::new(); req.body.read_to_string(&mut payload).unwrap(); // can we
核心
和Hyper的客户端
?我用的是hyper 0.11.0和tokio core 0.1
fn get_result(req: &mut Request) -> IronResult<Response> {
let mut payload = String::new();
req.body.read_to_string(&mut payload).unwrap();
// can we re-use core and client somehow. Making then global with lazy_static!() does not work.
let mut core = tokio_core::reactor::Core::new().unwrap();
let client = Client::new(&core.handle());
let uri = "http://host:port/getResult".parse().unwrap();
let mut req: hyper::Request = hyper::Request::new(hyper::Method::Post, uri);
req.headers_mut().set(ContentType::json());
req.headers_mut().set(ContentLength(payload.len() as u64));
req.set_body(payload);
let mut results: Vec<RequestFormat> = Vec::new();
let work = client.request(req).and_then(|res| {
res.body().for_each(|chunk| {
let re: ResultFormat = serde_json::from_slice(&chunk).unwrap();
results.push(re);
Ok(())
})
});
Ok(Response::with(
(iron::status::Ok, serde_json::to_string(&results).unwrap()),
))
}
fn获取结果(请求:&mut请求)->IronResult{
让mut payload=String::new();
req.body.read_to_字符串(&mut payload).unwrap();
//我们是否可以以某种方式重用核心和客户端。使用lazy_static!()使其全局化是行不通的。
让mut core=tokio_core::reactor::core::new().unwrap();
让client=client::new(&core.handle());
让uri=”http://host:port/getResult“.parse().unwrap();
让mut-req:hyper::Request=hyper::Request::new(hyper::Method::Post,uri);
req.headers_mut().set(ContentType::json());
req.headers_mut().set(ContentLength(payload.len()为u64));
要求设置机身(有效载荷);
让mut结果:Vec=Vec::new();
让work=client.request(req.),然后(| res |{
res.body()。用于每个(| chunk |{
让re:ResultFormat=serde_json::from_slice(&chunk).unwrap();
结果:推挤(re);
好(())
})
});
好的(回复::with(
(iron::status::Ok,serde_json::to_string(&results).unwrap()),
))
}
我创建了一个包装客户端和核心的Downloader类。下面是一个片段
use hyper;
use tokio_core;
use std::sync::{mpsc};
use std::thread;
use futures::Future;
use futures::stream::Stream;
use std::time::Duration;
use std::io::{self, Write};
use time::precise_time_ns;
use hyper::Client;
pub struct Downloader {
sender : mpsc::Sender<(hyper::Request, mpsc::Sender<hyper::Chunk>)>,
#[allow(dead_code)]
tr : thread::JoinHandle<hyper::Request>,
}
impl Downloader {
pub fn new() -> Downloader {
let (sender, receiver) = mpsc::channel::<(hyper::Request,mpsc::Sender<hyper::Chunk>)>();
let tr = thread::spawn(move||{
let mut core = tokio_core::reactor::Core::new().unwrap();
let client = Client::new(&core.handle());
loop {
let (req , sender) = receiver.recv().unwrap();
let begin = precise_time_ns();
let work = client.request(req)
.and_then(|res| {
res.body().for_each(|chunk| {
sender.send(chunk)
.map_err(|e|{
//io::sink().write(&chunk).unwrap();
io::Error::new(io::ErrorKind::Other, e)
})?;
Ok(())
})
//sender.close();
//res.body().concat2()
});
core.run(work).map_err(|e|{println!("Error Is {:?}", e);});
//This time prints same as all request processing time.
debug!("Time taken In Download {:?} ms", (precise_time_ns() - begin) / 1000000);
}
});
Downloader{sender,
tr,
}
}
pub fn download(&self, req : hyper::Request, results: mpsc::Sender<Vec<u8>>){
self.sender.send((req, results)).unwrap();
}
}
使用超链接;
使用tokio_核;
使用std::sync::{mpsc};
使用std::线程;
使用期货:未来;
使用futures::stream::stream;
使用std::time::Duration;
使用std::io::{self,Write};
使用时间:精确的时间;
使用hyper::Client;
发布结构下载程序{
发件人:mpsc::发件人,
#[允许(死代码)]
tr:thread::JoinHandle,
}
impl下载程序{
pub fn new()->下载程序{
let(发送方,接收方)=mpsc::channel::();
让tr=thread::spawn(移动||{
让mut core=tokio_core::reactor::core::new().unwrap();
让client=client::new(&core.handle());
环路{
let(req,sender)=receiver.recv().unwrap();
让我们开始=精确的时间();
让工作=客户端请求(req)
.然后| res |{
res.body()。用于每个(| chunk |{
sender.send(块)
.map|err(| e|{
//io::sink().write(&chunk).unwrap();
io::Error::new(io::ErrorKind::Other,e)
})?;
好(())
})
//sender.close();
//res.body().concat2()
});
core.run(work.map_err(| e |{println!(“错误为{:?}”,e);});
//此时间打印与所有请求处理时间相同。
调试!(“下载时所花费的时间{:?}ms”,(精确的\u Time\u ns()-begin)/1000000);
}
});
下载程序{发送器,
tr,
}
}
发布fn下载(&self,请求:hyper::Request,结果:mpsc::Sender){
self.sender.send((请求,结果)).unwrap();
}
}
现在这个类的客户机可以有一个静态变量
lazy_static!{
static ref DOWNLOADER : Mutex<downloader::Downloader> =
Mutex::new(downloader::Downloader::new());
}
let (sender, receiver) = mpsc::channel();
DOWNLOADER.lock().unwrap().download(payload, sender);
lazy\u static!{
静态引用下载程序:互斥=
Mutex::new(downloader::downloader::new());
}
let(发送方,接收方)=mpsc::channel();
DOWNLOADER.lock().unwrap().DOWNLOADER(有效载荷,发送方);
然后通过接收通道读取。
可能需要使用sender.drop()关闭发送方通道