Last active
August 13, 2018 10:32
-
-
Save syntheticsh/1058bcd51accb3ade57a4fb154f91000 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extern crate hyper; | |
| extern crate futures; | |
| extern crate tokio; | |
| use hyper::rt::{Future, Stream}; | |
| use futures::future::select_all; | |
| use futures::future::join_all; | |
| use futures::future::select_ok; | |
| use std::thread; | |
| use std::error::Error; | |
| use hyper::Chunk; | |
| use futures::{Sink, Async}; | |
| use futures::sync::mpsc::{Sender, channel}; | |
| use hyper::{Client, Method, Request, Body, Uri}; | |
| use std::time::Duration; | |
| fn main() { | |
| let (tx, mut rx) = channel::<Result<Chunk, String>>(1000); | |
| let handle = thread::spawn(move || { | |
| let mut i = 10; | |
| let mut futures = vec![]; | |
| while i != 0 { | |
| let tx_ok = Sender::clone(&tx); | |
| let tx_error = Sender::clone(&tx); | |
| let mut request = Request::new(Body::from(format!("{{\"test\":\"request{}\"}}", i))); | |
| *request.method_mut() = Method::POST; | |
| let mut uri_prep = "http://httpbin.org/post"; | |
| if i == 4 { | |
| uri_prep = "http://httpbin4.org/post"; | |
| } | |
| let uri: Uri = uri_prep.parse().unwrap(); | |
| *request.uri_mut() = uri; | |
| let post = Client::builder() | |
| .keep_alive_timeout(Duration::from_secs(3)) | |
| .build_http::<Body>(); | |
| let future = post | |
| .request(request) | |
| .and_then(|res| { | |
| res.into_body().concat2() | |
| }) | |
| .and_then(move |body| { | |
| tx_ok.send(Ok(body)).poll().unwrap(); | |
| Ok(()) | |
| }) | |
| .map_err(move |error| { | |
| tx_error.send(Err(String::from(error.description()))).poll().unwrap(); | |
| }); | |
| futures.push(future); | |
| i -= 1; | |
| } | |
| let work = select_ok(futures) | |
| .then(|_| { Ok(()) }); | |
| tokio::run(work); | |
| }); | |
| handle.join().unwrap(); | |
| while let Ok(Async::Ready(Some(value))) = rx.poll() { | |
| match value { | |
| Ok(result) => println!("{:?}", std::str::from_utf8(&result).unwrap()), | |
| Err(e) => {println!("first error {:?}", e)} | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment