Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http/1 payload read timeout #251

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
// 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)

Check warning on line 543 in ntex-io/src/dispatcher.rs

View check run for this annotation

Codecov / codecov/patch

ntex-io/src/dispatcher.rs#L543

Added line #L543 was not covered by tests
};

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 @@
} 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");

Check warning on line 556 in ntex-io/src/io.rs

View check run for this annotation

Codecov / codecov/patch

ntex-io/src/io.rs#L556

Added line #L556 was not covered by tests
}
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 @@ -118,7 +118,7 @@
if timeout.is_zero() {
self.headers_read_rate = None;
} else {
let mut rate = self.headers_read_rate.clone().unwrap_or_default();

Check warning on line 121 in ntex/src/http/config.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `Option<ReadRate>` which implements the `Copy` trait

warning: using `clone` on type `Option<ReadRate>` which implements the `Copy` trait --> ntex/src/http/config.rs:121:28 | 121 | let mut rate = self.headers_read_rate.clone().unwrap_or_default(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.headers_read_rate` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy = note: `#[warn(clippy::clone_on_copy)]` on by default
rate.timeout = timeout.into();
self.headers_read_rate = Some(rate);
}
Expand Down Expand Up @@ -258,7 +258,7 @@
upgrade: upgrade.map(|v| v.into()),
on_request: on_request.map(|v| v.into()),
keep_alive: Duration::from(cfg.keep_alive),
client_disconnect: cfg.client_disconnect.into(),

Check warning on line 261 in ntex/src/http/config.rs

View workflow job for this annotation

GitHub Actions / clippy

useless conversion to the same type: `ntex_util::time::Seconds`

warning: useless conversion to the same type: `ntex_util::time::Seconds` --> ntex/src/http/config.rs:261:32 | 261 | client_disconnect: cfg.client_disconnect.into(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `cfg.client_disconnect` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_conversion = note: `#[warn(clippy::useless_conversion)]` on by default
ka_enabled: cfg.ka_enabled,
headers_read_rate: cfg.headers_read_rate,
payload_read_rate: cfg.payload_read_rate,
Expand All @@ -275,10 +275,6 @@
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
Loading