forked from mirror/async-std
Http with io timeouts (#42)
* Add simple http example with a timeout * Update lib.rs simple http example comment * Move to current io module
This commit is contained in:
parent
2391db9003
commit
fa407b18c0
2 changed files with 72 additions and 0 deletions
36
README.md
36
README.md
|
@ -49,6 +49,42 @@ fn main() {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Low-Friction Sockets with Built-In Timeouts
|
||||||
|
|
||||||
|
```rust
|
||||||
|
#![feature(async_await)]
|
||||||
|
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use async_std::{
|
||||||
|
prelude::*,
|
||||||
|
task,
|
||||||
|
io,
|
||||||
|
net::TcpStream,
|
||||||
|
};
|
||||||
|
|
||||||
|
async fn get() -> io::Result<Vec<u8>> {
|
||||||
|
let mut stream = TcpStream::connect("example.com:80").await?;
|
||||||
|
stream.write_all(b"GET /index.html HTTP/1.0\r\n\r\n").await?;
|
||||||
|
|
||||||
|
let mut buf = vec![];
|
||||||
|
|
||||||
|
io::timeout(Duration::from_secs(5), async {
|
||||||
|
stream.read_to_end(&mut buf).await?
|
||||||
|
Ok(buf)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
task::block_on(async {
|
||||||
|
let raw_response = get().await.expect("request");
|
||||||
|
let response = String::from_utf8(raw_response)
|
||||||
|
.expect("utf8 conversion");
|
||||||
|
println!("received: {}", response);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Take a look around
|
## Take a look around
|
||||||
|
|
||||||
Clone the repo:
|
Clone the repo:
|
||||||
|
|
36
src/lib.rs
36
src/lib.rs
|
@ -22,6 +22,42 @@
|
||||||
//! })
|
//! })
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
//!
|
||||||
|
//! Use sockets in a familiar way, with low-friction built-in timeouts:
|
||||||
|
//!
|
||||||
|
//! ```no_run
|
||||||
|
//! #![feature(async_await)]
|
||||||
|
//!
|
||||||
|
//! use std::time::Duration;
|
||||||
|
//!
|
||||||
|
//! use async_std::{
|
||||||
|
//! prelude::*,
|
||||||
|
//! task,
|
||||||
|
//! io,
|
||||||
|
//! net::TcpStream,
|
||||||
|
//! };
|
||||||
|
//!
|
||||||
|
//! async fn get() -> io::Result<Vec<u8>> {
|
||||||
|
//! let mut stream = TcpStream::connect("example.com:80").await?;
|
||||||
|
//! stream.write_all(b"GET /index.html HTTP/1.0\r\n\r\n").await?;
|
||||||
|
//!
|
||||||
|
//! let mut buf = vec![];
|
||||||
|
//!
|
||||||
|
//! io::timeout(Duration::from_secs(5), async {
|
||||||
|
//! stream.read_to_end(&mut buf).await?
|
||||||
|
//! Ok(buf)
|
||||||
|
//! })
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! fn main() {
|
||||||
|
//! task::block_on(async {
|
||||||
|
//! let raw_response = get().await.expect("request");
|
||||||
|
//! let response = String::from_utf8(raw_response)
|
||||||
|
//! .expect("utf8 conversion");
|
||||||
|
//! println!("received: {}", response);
|
||||||
|
//! });
|
||||||
|
//! }
|
||||||
|
//! ```
|
||||||
|
|
||||||
#![feature(async_await)]
|
#![feature(async_await)]
|
||||||
#![cfg_attr(feature = "docs", feature(doc_cfg))]
|
#![cfg_attr(feature = "docs", feature(doc_cfg))]
|
||||||
|
|
Loading…
Reference in a new issue