Skip to content

Commit 25cf27f

Browse files
committed
refactor connection start
1 parent ca9e1fa commit 25cf27f

2 files changed

Lines changed: 65 additions & 59 deletions

File tree

src/connection.rs

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -248,70 +248,80 @@ impl Connection {
248248
executor.clone(),
249249
options.recovery_config.clone().unwrap_or_default(),
250250
);
251-
let status = conn.status.clone();
252-
let configuration = conn.configuration.clone();
253-
status.set_vhost(&uri.vhost);
254-
status.set_username(&uri.authority.userinfo.username);
251+
conn.configure(&uri);
252+
let stream = connect_promise
253+
.await
254+
.and_then(|stream| reactor.register(IOHandle::new(stream)).map_err(Into::into))
255+
.inspect_err(|_| {
256+
// We don't actually need the resolver as we already pass it around to the failing
257+
// code which will propagate the error. We only want to flush the status internal
258+
// state.
259+
let _ = conn.status.connection_resolver();
260+
})?
261+
.into();
262+
let heartbeat = Heartbeat::new(
263+
conn.status.clone(),
264+
conn.channels.clone(),
265+
executor.clone(),
266+
reactor,
267+
);
268+
let io_loop = IoLoop::new(
269+
conn.status.clone(),
270+
conn.configuration.clone(),
271+
conn.channels.clone(),
272+
internal_rpc.handle(),
273+
frames,
274+
socket_state,
275+
stream,
276+
heartbeat,
277+
);
278+
executor.spawn(Box::pin(internal_rpc.run(conn.channels.clone())));
279+
io_loop.start(&conn.io_loop)?;
280+
conn.start(uri, options).await
281+
}
282+
283+
fn configure(&self, uri: &AMQPUri) {
284+
self.status.set_vhost(&uri.vhost);
285+
self.status.set_username(&uri.authority.userinfo.username);
286+
255287
if let Some(frame_max) = uri.query.frame_max {
256-
configuration.set_frame_max(frame_max);
288+
self.configuration.set_frame_max(frame_max);
257289
}
258290
if let Some(channel_max) = uri.query.channel_max {
259-
configuration.set_channel_max(channel_max);
291+
self.configuration.set_channel_max(channel_max);
260292
}
261293
if let Some(heartbeat) = uri.query.heartbeat {
262-
configuration.set_heartbeat(heartbeat);
294+
self.configuration.set_heartbeat(heartbeat);
263295
}
264-
let (promise_out, resolver) = Promise::new();
296+
}
297+
298+
async fn start(self, uri: AMQPUri, options: ConnectionProperties) -> Result<Connection> {
299+
let (promise_out, resolver_out) = Promise::new();
300+
let (promise_in, resolver_in) = Promise::new();
265301
if level_enabled!(Level::TRACE) {
266302
promise_out.set_marker("ProtocolHeader".into());
303+
promise_in.set_marker("ProtocolHeader.Ok".into());
267304
}
268-
let channels = conn.channels.clone();
269-
if let Some(channel0) = channels.get(0) {
305+
306+
if let Some(channel0) = self.channels.get(0) {
270307
channel0.send_frame(
271308
AMQPFrame::ProtocolHeader(ProtocolVersion::amqp_0_9_1()),
272-
resolver,
309+
resolver_out,
273310
None,
274311
)
275-
};
276-
let (promise_in, resolver) = Promise::new();
277-
if level_enabled!(Level::TRACE) {
278-
promise_in.set_marker("ProtocolHeader.Ok".into());
279312
}
280-
let io_loop_handle = conn.io_loop.clone();
281-
status.set_state(ConnectionState::Connecting);
282-
status.set_connection_step(ConnectionStep::ProtocolHeader(
283-
resolver,
284-
conn,
285-
uri.authority.userinfo.into(),
286-
uri.query.auth_mechanism.unwrap_or_default(),
287-
options,
288-
));
289-
let stream = connect_promise
290-
.await
291-
.and_then(|stream| reactor.register(IOHandle::new(stream)).map_err(Into::into))
292-
.inspect_err(|_| {
293-
// We don't actually need the resolver as we already pass it around to the failing
294-
// code which will propagate the error. We only want to flush the status internal
295-
// state.
296-
let _ = status.connection_resolver();
297-
})?
298-
.into();
299-
let heartbeat = Heartbeat::new(status.clone(), channels.clone(), executor.clone(), reactor);
300-
let internal_rpc_handle = internal_rpc.handle();
301-
executor.spawn(Box::pin(internal_rpc.run(channels.clone())));
302-
IoLoop::new(
303-
status,
304-
configuration,
305-
channels,
306-
internal_rpc_handle,
307-
frames,
308-
socket_state,
309-
io_loop_handle,
310-
stream,
311-
heartbeat,
312-
)
313-
.await
314-
.and_then(IoLoop::start)?;
313+
314+
self.status.set_state(ConnectionState::Connecting);
315+
self.status
316+
.clone()
317+
.set_connection_step(ConnectionStep::ProtocolHeader(
318+
resolver_in,
319+
self,
320+
uri.authority.userinfo.into(),
321+
uri.query.auth_mechanism.unwrap_or_default(),
322+
options,
323+
));
324+
315325
promise_out.await?;
316326
promise_in.await
317327
}

src/io_loop.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub struct IoLoop {
4242
frames: Frames,
4343
heartbeat: Heartbeat,
4444
socket_state: SocketState,
45-
connection_io_loop_handle: ThreadHandle,
4645
stream: Pin<Box<dyn AsyncIOHandle + Send>>,
4746
status: Status,
4847
killswitch: KillSwitch,
@@ -53,40 +52,38 @@ pub struct IoLoop {
5352
}
5453

5554
impl IoLoop {
56-
pub(crate) async fn new(
55+
pub(crate) fn new(
5756
connection_status: ConnectionStatus,
5857
configuration: Configuration,
5958
channels: Channels,
6059
internal_rpc: InternalRPCHandle,
6160
frames: Frames,
6261
socket_state: SocketState,
63-
connection_io_loop_handle: ThreadHandle,
6462
stream: Pin<Box<dyn AsyncIOHandle + Send>>,
6563
heartbeat: Heartbeat,
66-
) -> Result<Self> {
64+
) -> Self {
6765
let frame_size = std::cmp::max(
6866
protocol::constants::FRAME_MIN_SIZE,
6967
configuration.frame_max(),
7068
);
7169
let killswitch = heartbeat.killswitch();
7270

73-
Ok(Self {
71+
Self {
7472
connection_status,
7573
configuration,
7674
channels,
7775
internal_rpc,
7876
frames,
7977
heartbeat,
8078
socket_state,
81-
connection_io_loop_handle,
8279
stream,
8380
status: Status::Initial,
8481
killswitch,
8582
frame_size,
8683
receive_buffer: Buffer::with_capacity(FRAMES_STORAGE * frame_size as usize),
8784
send_buffer: Buffer::with_capacity(FRAMES_STORAGE * frame_size as usize),
8885
serialized_frames: VecDeque::default(),
89-
})
86+
}
9087
}
9188

9289
fn readable_waker(&self) -> Waker {
@@ -156,9 +153,8 @@ impl IoLoop {
156153
}
157154
}
158155

159-
pub fn start(mut self) -> Result<()> {
156+
pub fn start(mut self, handle: &ThreadHandle) -> Result<()> {
160157
let waker = self.socket_state.handle();
161-
let handle = self.connection_io_loop_handle.clone();
162158
let current_span = tracing::Span::current();
163159
handle.register(
164160
ThreadBuilder::new()

0 commit comments

Comments
 (0)