Skip to content

Commit ffda263

Browse files
authored
perf(body): reuse tokio::time::Sleep timer via reset() in ReadTimeoutBody (#3085)
1 parent 4e9a3c7 commit ffda263

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

src/async_impl/body.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ where
339339
) -> Poll<Option<Result<hyper::body::Frame<Self::Data>, Self::Error>>> {
340340
let mut this = self.project();
341341

342-
// Start the `Sleep` if not active.
342+
let deadline = tokio::time::Instant::now() + *this.timeout;
343343
let sleep_pinned = if let Some(some) = this.sleep.as_mut().as_pin_mut() {
344344
some
345345
} else {
346-
this.sleep.set(Some(tokio::time::sleep(*this.timeout)));
346+
this.sleep.set(Some(tokio::time::sleep_until(deadline)));
347347
this.sleep.as_mut().as_pin_mut().unwrap()
348348
};
349349

@@ -354,8 +354,10 @@ where
354354

355355
let item = ready!(this.inner.poll_frame(cx))
356356
.map(|opt_chunk| opt_chunk.map_err(crate::error::body));
357-
// a ready frame means timeout is reset
358-
this.sleep.set(None);
357+
// a ready frame means timeout is reset for the next chunk
358+
if let Some(some) = this.sleep.as_mut().as_pin_mut() {
359+
some.reset(tokio::time::Instant::now() + *this.timeout);
360+
}
359361
Poll::Ready(item)
360362
}
361363

0 commit comments

Comments
 (0)