Skip to content

Commit 9aafe99

Browse files
authored
Refactor control messages (#79)
1 parent 77a5d12 commit 9aafe99

7 files changed

Lines changed: 109 additions & 107 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [3.6.0] - 2026-02-02
4+
5+
* Refactor control messages
6+
37
## [3.5.0] - 2026-01-29
48

59
* Use ntex_dispatcher::Dispatcher instead of ntex-io

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-h2"
3-
version = "3.5.0"
3+
version = "3.6.0"
44
license = "MIT OR Apache-2.0"
55
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
66
description = "An HTTP/2 client and server"
@@ -27,7 +27,7 @@ ntex-bytes = "1.4"
2727
ntex-codec = "1.1"
2828
ntex-dispatcher = "3"
2929
ntex-http = "1"
30-
ntex-io = "3.5"
30+
ntex-io = "3.7"
3131
ntex-net = "3"
3232
ntex-service = "4"
3333
ntex-util = "3"
@@ -50,7 +50,7 @@ walkdir = "2.3.2"
5050
serde = "1"
5151
serde_json = "1"
5252

53-
ntex = { version = "3.0.0-pre.14", features = ["openssl"] }
53+
ntex = { version = "3.0.0-pre.15", features = ["openssl"] }
5454
ntex-tls = { version = "3", features = ["openssl"] }
5555
openssl = "0.10"
5656

src/connection.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -380,13 +380,6 @@ impl Connection {
380380
Ok(stream.into_stream())
381381
}
382382

383-
pub(crate) fn rst_stream(&self, id: StreamId, reason: frame::Reason) {
384-
let stream = self.0.streams.borrow_mut().get(&id).cloned();
385-
if let Some(stream) = stream {
386-
stream.set_failed(Some(reason))
387-
}
388-
}
389-
390383
pub(crate) fn drop_stream(&self, id: StreamId) {
391384
let empty = {
392385
let mut streams = self.0.streams.borrow_mut();
@@ -1035,7 +1028,7 @@ mod tests {
10351028
msg.stream().reset(Reason::REFUSED_STREAM);
10361029
Ok::<_, h2::StreamError>(())
10371030
}),
1038-
fn_service(|msg: h2::ControlMessage<h2::StreamError>| async move {
1031+
fn_service(|msg: h2::Control<h2::StreamError>| async move {
10391032
Ok::<_, ()>(msg.ack())
10401033
}),
10411034
)
@@ -1082,7 +1075,7 @@ mod tests {
10821075
msg.stream().reset(Reason::NO_ERROR);
10831076
Ok::<_, h2::StreamError>(())
10841077
}),
1085-
fn_service(|msg: h2::ControlMessage<h2::StreamError>| async move {
1078+
fn_service(|msg: h2::Control<h2::StreamError>| async move {
10861079
Ok::<_, ()>(msg.ack())
10871080
}),
10881081
)

src/control.rs

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,96 @@
11
use std::io;
22

3-
use crate::frame::{Frame, Reason, Reset};
3+
use crate::frame::Frame;
44
use crate::{error, frame, stream::StreamRef};
55

6-
#[doc(hidden)]
7-
pub type ControlMessage<E> = Control<E>;
8-
#[doc(hidden)]
9-
pub type ControlResult = ControlAck;
10-
116
#[derive(Debug)]
127
pub enum Control<E> {
8+
/// Connection is prepared to disconnect
9+
Disconnect(Reason<E>),
10+
/// Protocol dispatcher is terminated
11+
Terminated(Terminated),
12+
}
13+
14+
#[derive(Debug)]
15+
/// Disconnect reason
16+
pub enum Reason<E> {
1317
/// Application level error from publish service
14-
AppError(AppError<E>),
18+
Error(Error<E>),
1519
/// Protocol level error
16-
ConnectionError(ConnectionError),
20+
ProtocolError(ConnectionError),
1721
/// Remote GoAway is received
1822
GoAway(GoAway),
1923
/// Peer is gone
2024
PeerGone(PeerGone),
21-
/// Protocol dispatcher is terminated
22-
Terminated(Terminated),
2325
}
2426

2527
#[derive(Clone, Debug)]
2628
pub struct ControlAck {
2729
pub(crate) frame: Option<Frame>,
28-
pub(crate) disconnect: bool,
2930
}
3031

3132
impl<E> Control<E> {
3233
/// Create a new `Control` message for app level errors
33-
pub(super) fn error(err: E) -> Self {
34-
Control::AppError(AppError::new(err, None))
35-
}
36-
37-
/// Create a new `Control` message for app level errors
38-
pub(super) fn app_error(err: E, stream: StreamRef) -> Self {
39-
Control::AppError(AppError::new(err, Some(stream)))
34+
pub(super) fn error(err: E, stream: Option<StreamRef>) -> Self {
35+
Control::Disconnect(Reason::Error(Error::new(err, stream)))
4036
}
4137

4238
/// Create a new `Control` message from GOAWAY packet.
4339
pub(super) fn go_away(frm: frame::GoAway) -> Self {
44-
Control::GoAway(GoAway(frm))
40+
Control::Disconnect(Reason::GoAway(GoAway(frm)))
4541
}
4642

4743
/// Create a new `Control` message from DISCONNECT packet.
4844
pub(super) fn peer_gone(err: Option<io::Error>) -> Self {
49-
Control::PeerGone(PeerGone(err))
45+
Control::Disconnect(Reason::PeerGone(PeerGone(err)))
46+
}
47+
48+
/// Create a new `Control` message for protocol level errors
49+
pub(super) fn proto_error(err: error::ConnectionError) -> Self {
50+
Control::Disconnect(Reason::ProtocolError(ConnectionError::new(err)))
5051
}
5152

5253
pub(super) fn terminated() -> Self {
5354
Control::Terminated(Terminated)
5455
}
5556

56-
/// Create a new `Control` message for protocol level errors
57-
pub(super) fn proto_error(err: error::ConnectionError) -> Self {
58-
Control::ConnectionError(ConnectionError::new(err))
57+
/// Default ack impl
58+
pub fn ack(self) -> ControlAck {
59+
match self {
60+
Control::Disconnect(item) => item.ack(),
61+
Control::Terminated(item) => item.ack(),
62+
}
5963
}
64+
}
6065

66+
impl<E> Reason<E> {
6167
/// Default ack impl
6268
pub fn ack(self) -> ControlAck {
6369
match self {
64-
Control::AppError(item) => item.ack(),
65-
Control::ConnectionError(item) => item.ack(),
66-
Control::GoAway(item) => item.ack(),
67-
Control::PeerGone(item) => item.ack(),
68-
Control::Terminated(item) => item.ack(),
70+
Reason::Error(item) => item.ack(),
71+
Reason::ProtocolError(item) => item.ack(),
72+
Reason::GoAway(item) => item.ack(),
73+
Reason::PeerGone(item) => item.ack(),
6974
}
7075
}
7176
}
7277

73-
/// Service level error
78+
/// Application level error
7479
#[derive(Debug)]
75-
pub struct AppError<E> {
80+
pub struct Error<E> {
7681
err: E,
77-
reason: Reason,
78-
stream: Option<StreamRef>,
82+
goaway: frame::GoAway,
7983
}
8084

81-
impl<E> AppError<E> {
85+
impl<E> Error<E> {
8286
fn new(err: E, stream: Option<StreamRef>) -> Self {
83-
Self {
84-
err,
85-
stream,
86-
reason: Reason::CANCEL,
87-
}
87+
let goaway = if let Some(ref stream) = stream {
88+
frame::GoAway::new(frame::Reason::INTERNAL_ERROR).set_last_stream_id(stream.id())
89+
} else {
90+
frame::GoAway::new(frame::Reason::INTERNAL_ERROR)
91+
};
92+
93+
Self { err, goaway }
8894
}
8995

9096
#[inline]
@@ -95,24 +101,16 @@ impl<E> AppError<E> {
95101

96102
#[inline]
97103
/// Set reason code for go away packet
98-
pub fn reason(mut self, reason: Reason) -> Self {
99-
self.reason = reason;
104+
pub fn reason(mut self, reason: frame::Reason) -> Self {
105+
self.goaway = self.goaway.set_reason(reason);
100106
self
101107
}
102108

103109
#[inline]
104110
/// Ack service error, return disconnect packet and close connection.
105111
pub fn ack(self) -> ControlAck {
106-
if let Some(ref stream) = self.stream {
107-
ControlAck {
108-
frame: Some(Reset::new(stream.id(), self.reason).into()),
109-
disconnect: false,
110-
}
111-
} else {
112-
ControlAck {
113-
frame: None,
114-
disconnect: true,
115-
}
112+
ControlAck {
113+
frame: Some(self.goaway.into()),
116114
}
117115
}
118116
}
@@ -125,10 +123,7 @@ impl Terminated {
125123
#[inline]
126124
/// convert packet to a result
127125
pub fn ack(self) -> ControlAck {
128-
ControlAck {
129-
frame: None,
130-
disconnect: true,
131-
}
126+
ControlAck { frame: None }
132127
}
133128
}
134129

@@ -155,7 +150,7 @@ impl ConnectionError {
155150

156151
#[inline]
157152
/// Set reason code for go away packet
158-
pub fn reason(mut self, reason: Reason) -> Self {
153+
pub fn reason(mut self, reason: frame::Reason) -> Self {
159154
self.frm = self.frm.set_reason(reason);
160155
self
161156
}
@@ -165,7 +160,6 @@ impl ConnectionError {
165160
pub fn ack(self) -> ControlAck {
166161
ControlAck {
167162
frame: Some(self.frm.into()),
168-
disconnect: true,
169163
}
170164
}
171165
}
@@ -185,10 +179,7 @@ impl PeerGone {
185179
}
186180

187181
pub fn ack(self) -> ControlAck {
188-
ControlAck {
189-
frame: None,
190-
disconnect: true,
191-
}
182+
ControlAck { frame: None }
192183
}
193184
}
194185

@@ -202,9 +193,6 @@ impl GoAway {
202193
}
203194

204195
pub fn ack(self) -> ControlAck {
205-
ControlAck {
206-
frame: None,
207-
disconnect: true,
208-
}
196+
ControlAck { frame: None }
209197
}
210198
}

0 commit comments

Comments
 (0)