@@ -26,6 +26,7 @@ use tracing::{Level, debug, error, level_enabled, trace};
2626#[ derive( Clone ) ]
2727pub ( 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 }
0 commit comments