Skip to content

Commit 7d537a8

Browse files
committed
Proper keep-alive
1 parent 3fed77b commit 7d537a8

File tree

2 files changed

+21
-32
lines changed

2 files changed

+21
-32
lines changed

examples/server.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use async_h1::{server, Body};
2-
use async_std::prelude::*;
32
use async_std::net;
3+
use async_std::prelude::*;
44
use async_std::task;
55

66
fn main() -> Result<(), async_h1::Exception> {
@@ -10,12 +10,15 @@ fn main() -> Result<(), async_h1::Exception> {
1010
let mut incoming = listener.incoming();
1111

1212
while let Some(stream) = incoming.next().await {
13+
println!("new TCP stream...");
1314
task::spawn(async {
1415
let stream = stream?;
1516
let (reader, writer) = &mut (&stream, &stream);
16-
server::connect(reader, writer, |_| async {
17-
let body = Body::from_string("hello chashu".to_owned());
18-
Ok(http::Response::new(body))
17+
server::connect(reader, writer, |_| {
18+
async {
19+
let body = Body::from_string("hello chashu".to_owned());
20+
Ok(http::Response::new(body))
21+
}
1922
})
2023
.await
2124
});

src/server.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Process HTTP connections on the server.
22
3-
use async_std::future::Future;
3+
use async_std::future::{timeout, Future, TimeoutError};
44
use async_std::io::{self, BufReader};
55
use async_std::io::{Read, Write};
66
use async_std::prelude::*;
77
use async_std::task::{Context, Poll};
88
use futures_core::ready;
99
use http::{Request, Response, Version};
10-
use std::time::{Duration, Instant};
10+
use std::time::Duration;
1111

1212
use std::pin::Pin;
1313

@@ -26,35 +26,27 @@ where
2626
O: Read + Unpin + Send,
2727
{
2828
let req = decode(reader).await?;
29-
if let RequestOrReader::Request(mut req) = req {
29+
if let Some(mut req) = req {
3030
let headers = req.headers();
31-
let timeout = match (headers.get("Connection"), headers.get("Keep-Alive")) {
31+
let timeout_duration = match (headers.get("Connection"), headers.get("Keep-Alive")) {
3232
(Some(connection), Some(_v))
3333
if connection == http::header::HeaderValue::from_static("Keep-Alive") =>
3434
{
3535
// TODO: parse timeout
36-
Duration::from_secs(5)
36+
Duration::from_secs(10)
3737
}
38-
_ => Duration::from_secs(5),
38+
_ => Duration::from_secs(10),
3939
};
4040

41-
let beginning = Instant::now();
4241
loop {
4342
// TODO: what to do when the callback returns Err
4443
let mut res = encode(callback(&mut req).await?).await?;
4544
io::copy(&mut res, writer).await?;
46-
let mut stream = req.into_body().into_reader().into_inner();
47-
req = loop {
48-
match decode(stream).await? {
49-
RequestOrReader::Request(r) => break r,
50-
RequestOrReader::Reader(r) => {
51-
let now = Instant::now();
52-
if now - beginning > timeout {
53-
return Ok(());
54-
}
55-
stream = r;
56-
}
57-
}
45+
let stream = req.into_body().into_reader().into_inner();
46+
req = match timeout(timeout_duration, decode(stream)).await {
47+
Ok(Ok(Some(r))) => r,
48+
Ok(Ok(None)) | Err(TimeoutError { .. }) => break, /* EOF or timeout */
49+
Ok(Err(e)) => return Err(e),
5850
};
5951
}
6052
}
@@ -165,14 +157,8 @@ where
165157
Ok(Encoder::new(buf, res.into_body()))
166158
}
167159

168-
#[derive(Debug)]
169-
pub enum RequestOrReader<R: Read> {
170-
Request(Request<Body<BufReader<R>>>),
171-
Reader(R),
172-
}
173-
174160
/// Decode an HTTP request on the server.
175-
pub async fn decode<R>(reader: R) -> Result<RequestOrReader<R>, Exception>
161+
pub async fn decode<R>(reader: R) -> Result<Option<Request<Body<BufReader<R>>>>, Exception>
176162
where
177163
R: Read + Unpin + Send,
178164
{
@@ -186,7 +172,7 @@ where
186172
let bytes_read = reader.read_until(b'\n', &mut buf).await?;
187173
// No more bytes are yielded from the stream.
188174
if bytes_read == 0 {
189-
return Ok(RequestOrReader::Reader(reader.into_inner()));
175+
return Ok(None);
190176
}
191177

192178
// We've hit the end delimiter of the stream.
@@ -232,5 +218,5 @@ where
232218
};
233219

234220
// Return the request.
235-
Ok(RequestOrReader::Request(req.body(body)?))
221+
Ok(Some(req.body(body)?))
236222
}

0 commit comments

Comments
 (0)