Stop write task if io is closed (#416)

This commit is contained in:
Nikolay Kim 2024-09-07 10:04:08 +05:00 committed by GitHub
parent 3edb54ffdf
commit db6d3a6e4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 59 additions and 25 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "ntex-io"
version = "2.3.1"
version = "2.4.0"
authors = ["ntex contributors <team@ntex.rs>"]
description = "Utilities for encoding and decoding frames"
keywords = ["network", "framework", "async", "futures"]

View file

@ -97,25 +97,26 @@ impl Filter for Base {
if flags.contains(Flags::IO_STOPPED) {
Poll::Ready(WriteStatus::Terminate)
} else if flags.intersects(Flags::IO_STOPPING) {
Poll::Ready(WriteStatus::Shutdown(
self.0 .0.disconnect_timeout.get().into(),
))
} else if flags.contains(Flags::IO_STOPPING_FILTERS)
&& !flags.contains(Flags::IO_FILTERS_TIMEOUT)
{
flags.insert(Flags::IO_FILTERS_TIMEOUT);
self.0.set_flags(flags);
self.0 .0.write_task.register(cx.waker());
Poll::Ready(WriteStatus::Timeout(
self.0 .0.disconnect_timeout.get().into(),
))
} else if flags.intersects(Flags::WR_PAUSED) {
self.0 .0.write_task.register(cx.waker());
Poll::Pending
} else {
self.0 .0.write_task.register(cx.waker());
Poll::Ready(WriteStatus::Ready)
if flags.intersects(Flags::IO_STOPPING) {
Poll::Ready(WriteStatus::Shutdown(
self.0 .0.disconnect_timeout.get().into(),
))
} else if flags.contains(Flags::IO_STOPPING_FILTERS)
&& !flags.contains(Flags::IO_FILTERS_TIMEOUT)
{
flags.insert(Flags::IO_FILTERS_TIMEOUT);
self.0.set_flags(flags);
Poll::Ready(WriteStatus::Timeout(
self.0 .0.disconnect_timeout.get().into(),
))
} else if flags.intersects(Flags::WR_PAUSED) {
Poll::Pending
} else {
Poll::Ready(WriteStatus::Ready)
}
}
}

View file

@ -41,6 +41,10 @@ impl IoRef {
.intersects(Flags::IO_STOPPING | Flags::IO_STOPPED)
}
pub(crate) fn is_io_closed(&self) -> bool {
self.0.flags.get().intersects(Flags::IO_STOPPED)
}
#[inline]
/// Check if write back-pressure is enabled
pub fn is_wr_backpressure(&self) -> bool {

View file

@ -296,6 +296,17 @@ impl WriteContext {
self.0.filter().poll_write_ready(cx)
}
#[inline]
/// Check if io is closed
pub fn poll_close(&self, cx: &mut Context<'_>) -> Poll<()> {
if self.0.is_io_closed() {
Poll::Ready(())
} else {
self.0 .0.write_task.register(cx.waker());
Poll::Pending
}
}
/// Get write buffer
pub fn with_buf<F>(&self, f: F) -> Poll<io::Result<()>>
where