Skip to content

Commit 67afeee

Browse files
committed
rework channel0 internal handling
1 parent 1131078 commit 67afeee

2 files changed

Lines changed: 66 additions & 71 deletions

File tree

src/channels.rs

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use tracing::{Level, debug, error, level_enabled, trace};
2626
#[derive(Clone)]
2727
pub(crate) struct Channels {
2828
inner: Arc<Mutex<Inner>>,
29+
channel0: Channel,
2930
configuration: Configuration,
3031
connection_status: ConnectionStatus,
3132
internal_rpc: InternalRPCHandle,
@@ -49,12 +50,24 @@ impl Channels {
4950
uri: AMQPUri,
5051
options: ConnectionProperties,
5152
) -> Self {
53+
let mut inner = Inner::new(
54+
configuration.clone(),
55+
waker,
56+
options.recovery_config.clone().unwrap_or_default(),
57+
);
58+
let channel0 = inner.create_channel(
59+
0,
60+
connection_status.clone(),
61+
internal_rpc.clone(),
62+
frames.clone(),
63+
executor.clone(),
64+
None,
65+
);
66+
channel0.set_state(ChannelState::Connected);
67+
5268
Self {
53-
inner: Arc::new(Mutex::new(Inner::new(
54-
configuration.clone(),
55-
waker,
56-
options.recovery_config.clone().unwrap_or_default(),
57-
))),
69+
inner: Arc::new(Mutex::new(inner)),
70+
channel0,
5871
configuration,
5972
connection_status,
6073
internal_rpc,
@@ -77,17 +90,8 @@ impl Channels {
7790
)
7891
}
7992

80-
pub(crate) fn create_zero(&self) {
81-
self.lock_inner()
82-
.create_channel(
83-
0,
84-
self.connection_status.clone(),
85-
self.internal_rpc.clone(),
86-
self.frames.clone(),
87-
self.executor.clone(),
88-
None,
89-
)
90-
.set_state(ChannelState::Connected);
93+
pub(crate) fn channel0(&self) -> Channel {
94+
self.channel0.clone()
9195
}
9296

9397
pub(crate) fn get(&self, id: ChannelId) -> Option<Channel> {
@@ -96,6 +100,11 @@ impl Channels {
96100

97101
pub(crate) fn remove(&self, id: ChannelId, error: Error) -> Result<()> {
98102
self.frames.clear_expected_replies(id, error);
103+
104+
if id == 0 {
105+
return Ok(());
106+
}
107+
99108
if self.lock_inner().channels.remove(&id).is_some() {
100109
Ok(())
101110
} else {
@@ -104,9 +113,13 @@ impl Channels {
104113
}
105114

106115
pub(crate) fn receive_method(&self, id: ChannelId, method: AMQPClass) -> Result<()> {
107-
self.get(id)
108-
.map(|channel| channel.receive_method(method))
109-
.unwrap_or_else(|| Err(ErrorKind::InvalidChannel(id).into()))
116+
if id == 0 {
117+
self.channel0.receive_method(method)
118+
} else {
119+
self.get(id)
120+
.map(|channel| channel.receive_method(method))
121+
.unwrap_or_else(|| Err(ErrorKind::InvalidChannel(id).into()))
122+
}
110123
}
111124

112125
pub(crate) fn handle_content_header_frame(
@@ -172,16 +185,15 @@ impl Channels {
172185
pub(crate) fn send_heartbeat(&self) {
173186
debug!("send heartbeat");
174187

175-
if let Some(channel0) = self.get(0) {
176-
let (promise, resolver) = Promise::new();
177-
178-
if level_enabled!(Level::TRACE) {
179-
promise.set_marker("Heartbeat".into());
180-
}
188+
let (promise, resolver) = Promise::new();
181189

182-
channel0.send_frame(AMQPFrame::Heartbeat(0), resolver, None);
183-
self.internal_rpc.register_internal_future(promise);
190+
if level_enabled!(Level::TRACE) {
191+
promise.set_marker("Heartbeat".into());
184192
}
193+
194+
self.channel0
195+
.send_frame(AMQPFrame::Heartbeat(0), resolver, None);
196+
self.internal_rpc.register_internal_future(promise);
185197
}
186198

187199
pub(crate) fn handle_frame(&self, f: AMQPFrame) -> Result<()> {
@@ -220,7 +232,8 @@ impl Channels {
220232
AMQPHardError::FRAMEERROR.into(),
221233
format!("heartbeat frame received on channel {channel_id}").into(),
222234
);
223-
if let Some(channel0) = self.get(0) {
235+
let channel0 = self.channel0();
236+
{
224237
let error = error.clone();
225238
self.internal_rpc.register_internal_future(async move {
226239
channel0
@@ -243,7 +256,8 @@ impl Channels {
243256
AMQPHardError::CHANNELERROR.into(),
244257
format!("content header frame received on channel {channel_id}").into(),
245258
);
246-
if let Some(channel0) = self.get(0) {
259+
let channel0 = self.channel0();
260+
{
247261
let error = error.clone();
248262
self.internal_rpc.register_internal_future(async move {
249263
channel0
@@ -292,7 +306,6 @@ impl Channels {
292306
self.lock_inner()
293307
.channels
294308
.values()
295-
.filter(|c| c.id() != 0)
296309
.fold(Some(error), |error, channel| {
297310
channel.init_recovery_or_shutdown(error)
298311
});
@@ -303,7 +316,6 @@ impl Channels {
303316
.lock_inner()
304317
.channels
305318
.values()
306-
.filter(|c| c.id() != 0)
307319
.cloned()
308320
.collect::<Vec<_>>();
309321

@@ -390,7 +402,7 @@ impl Inner {
390402
connection_closer: Option<Arc<ConnectionCloser>>,
391403
) -> Channel {
392404
debug!(%id, "create channel");
393-
let channel = Channel::new(
405+
Channel::new(
394406
id,
395407
self.configuration.clone(),
396408
connection_status,
@@ -400,9 +412,7 @@ impl Inner {
400412
executor,
401413
connection_closer,
402414
self.recovery_config.clone(),
403-
);
404-
self.channels.insert(id, channel.clone_internal());
405-
channel
415+
)
406416
}
407417

408418
fn create(
@@ -426,14 +436,16 @@ impl Inner {
426436
met_first_id = true;
427437
}
428438
if !self.channels.contains_key(&id) {
429-
return Ok(self.create_channel(
439+
let channel = self.create_channel(
430440
id,
431441
connection_status,
432442
internal_rpc,
433443
frames,
434444
executor,
435445
Some(connection_closer),
436-
));
446+
);
447+
self.channels.insert(id, channel.clone_internal());
448+
return Ok(channel);
437449
}
438450
id = self.channel_id.next();
439451
}

src/connection.rs

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,13 @@ impl Connection {
6565
options,
6666
);
6767
let closer = Arc::new(ConnectionCloser::new(status.clone(), internal_rpc, false));
68-
let connection = Self {
68+
Self {
6969
configuration,
7070
status,
7171
channels,
7272
io_loop: ThreadHandle::default(),
7373
closer,
74-
};
75-
76-
connection.channels.create_zero();
77-
connection
74+
}
7875
}
7976

8077
pub(crate) fn for_reconnect(
@@ -178,40 +175,28 @@ impl Connection {
178175
}
179176

180177
self.channels.set_connection_closing();
181-
if let Some(channel0) = self.channels.get(0) {
182-
channel0
183-
.connection_close(reply_code, reply_text, 0, 0)
184-
.await
185-
} else {
186-
Ok(())
187-
}
178+
self.channels
179+
.channel0()
180+
.connection_close(reply_code, reply_text, 0, 0)
181+
.await
188182
}
189183

190184
/// Block all consumers and publishers on this connection
191185
pub async fn block(&self, reason: &str) -> Result<()> {
192-
if let Some(channel0) = self.channels.get(0) {
193-
channel0.connection_blocked(reason).await
194-
} else {
195-
Err(ErrorKind::InvalidConnectionState(self.status.state()).into())
196-
}
186+
self.channels.channel0().connection_blocked(reason).await
197187
}
198188

199189
/// Unblock all consumers and publishers on this connection
200190
pub async fn unblock(&self) -> Result<()> {
201-
if let Some(channel0) = self.channels.get(0) {
202-
channel0.connection_unblocked().await
203-
} else {
204-
Err(ErrorKind::InvalidConnectionState(self.status.state()).into())
205-
}
191+
self.channels.channel0().connection_unblocked().await
206192
}
207193

208194
/// Update the secret used by some authentication module such as OAuth2
209195
pub async fn update_secret(&self, new_secret: &str, reason: &str) -> Result<()> {
210-
if let Some(channel0) = self.channels.get(0) {
211-
channel0.connection_update_secret(new_secret, reason).await
212-
} else {
213-
Err(ErrorKind::InvalidConnectionState(self.status.state()).into())
214-
}
196+
self.channels
197+
.channel0()
198+
.connection_update_secret(new_secret, reason)
199+
.await
215200
}
216201

217202
pub async fn connector(
@@ -305,13 +290,11 @@ impl Connection {
305290
promise_in.set_marker("ProtocolHeader.Ok".into());
306291
}
307292

308-
if let Some(channel0) = self.channels.get(0) {
309-
channel0.send_frame(
310-
AMQPFrame::ProtocolHeader(ProtocolVersion::amqp_0_9_1()),
311-
resolver_out,
312-
None,
313-
)
314-
}
293+
self.channels.channel0().send_frame(
294+
AMQPFrame::ProtocolHeader(ProtocolVersion::amqp_0_9_1()),
295+
resolver_out,
296+
None,
297+
);
315298

316299
self.status.set_state(ConnectionState::Connecting);
317300
self.status

0 commit comments

Comments
 (0)