Skip to content

Commit

Permalink
Add payload read timeout (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
fafhrd91 authored Nov 20, 2023
1 parent d441e79 commit fda7e41
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 105 deletions.
14 changes: 10 additions & 4 deletions ntex-io/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -537,19 +537,25 @@ where
// update read timer
if let Some((_, max, rate)) = self.cfg.frame_read_rate() {
let bytes = decoded.remains as u32;
let delta = if bytes > self.read_bytes {
(bytes - self.read_bytes).try_into().unwrap_or(u16::MAX)
} else {
bytes.try_into().unwrap_or(u16::MAX)
};

let delta = (bytes - self.read_bytes).try_into().unwrap_or(u16::MAX);

// read rate higher than min rate
if delta >= rate {
let n = now();
let next = self.shared.io.timer_deadline() + ONE_SEC;
let new_timeout = if n >= next { ONE_SEC } else { next - n };

// max timeout
// extend timeout
if max.is_zero() || (n + new_timeout) <= self.read_max_timeout {
self.read_bytes = bytes;
self.shared.io.stop_timer();
self.shared.io.start_timer(new_timeout);

// store current buf size for future rate calculation
self.read_bytes = bytes;
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion ntex-io/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,9 @@ impl<F> Io<F> {
} else {
match self.poll_read_ready(cx) {
Poll::Pending | Poll::Ready(Ok(Some(()))) => {
log::debug!("not enough data to decode next frame");
if log::log_enabled!(log::Level::Debug) && decoded.remains != 0 {
log::debug!("not enough data to decode next frame");
}
Ok(decoded)
}
Poll::Ready(Err(e)) => Err(RecvError::PeerGone(Some(e))),
Expand Down
4 changes: 3 additions & 1 deletion ntex/CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# Changes

## [0.7.11] - 2023-11-xx
## [0.7.11] - 2023-11-20

* Refactor http/1 timeouts

* Add http/1 payload read timeout

## [0.7.10] - 2023-11-12

* Start http client timeout after sending body
Expand Down
4 changes: 0 additions & 4 deletions ntex/src/http/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,6 @@ impl<S, X, U> DispatcherConfig<S, X, U> {
pub(super) fn headers_read_rate(&self) -> Option<&ReadRate> {
self.headers_read_rate.as_ref()
}

pub(super) fn payload_read_rate(&self) -> Option<&ReadRate> {
self.payload_read_rate.as_ref()
}
}

const DATE_VALUE_LENGTH_HDR: usize = 39;
Expand Down
Loading

0 comments on commit fda7e41

Please sign in to comment.