Skip to content

Commit 8fc29e1

Browse files
committed
Make active session state creation nicer by moving it into a dedicated function
1 parent 2f6a259 commit 8fc29e1

2 files changed

Lines changed: 17 additions & 17 deletions

File tree

crates/hotfix/src/session.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::message::sequence_reset::SequenceReset;
3131
use crate::message::test_request::TestRequest;
3232
use crate::message_utils::is_admin;
3333
use crate::session::event::AwaitingActiveSessionResponse;
34-
use crate::session::state::{ActiveState, AwaitingResendState};
34+
use crate::session::state::AwaitingResendState;
3535
use crate::session_schedule::SessionSchedule;
3636
use event::SessionEvent;
3737
use hotfix_message::parsed_message::ParsedMessage;
@@ -253,12 +253,8 @@ impl<M: FixMessage, S: MessageStore> Session<M, S> {
253253
async fn check_end_of_resend(&mut self) -> Result<()> {
254254
let ended_state = if let SessionState::AwaitingResend(state) = &mut self.state {
255255
if self.store.next_target_seq_number() > state.end_seq_number {
256-
let new_state = SessionState::Active(ActiveState {
257-
writer: state.writer.clone(),
258-
heartbeat_timer: Box::pin(sleep(Duration::from_secs(
259-
self.config.heartbeat_interval,
260-
))),
261-
});
256+
let new_state =
257+
SessionState::new_active(state.writer.clone(), self.config.heartbeat_interval);
262258
Some(std::mem::replace(&mut self.state, new_state))
263259
} else {
264260
None
@@ -356,12 +352,8 @@ impl<M: FixMessage, S: MessageStore> Session<M, S> {
356352
match self.verify_message(message).await {
357353
Ok(_) => {
358354
// happy logon flow, the session is now active
359-
self.state = SessionState::Active(ActiveState {
360-
writer: writer.clone(),
361-
heartbeat_timer: Box::pin(sleep(Duration::from_secs(
362-
self.config.heartbeat_interval,
363-
))),
364-
})
355+
self.state =
356+
SessionState::new_active(writer.clone(), self.config.heartbeat_interval);
365357
}
366358
Err(err) => match err {
367359
MessageVerificationError::SeqNumberTooLow { actual, expected } => {
@@ -746,7 +738,7 @@ where
746738
}
747739
}
748740
() = async {
749-
if let Some(ref mut timer) = session.state.heartbeat_timer() {
741+
if let Some(timer) = session.state.heartbeat_timer() {
750742
timer.as_mut().await
751743
} else {
752744
std::future::pending().await

crates/hotfix/src/session/state.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::collections::VecDeque;
66
use std::pin::Pin;
77
use std::time::Duration;
88
use tokio::sync::oneshot;
9-
use tokio::time::{Instant, Sleep};
9+
use tokio::time::{Instant, Sleep, sleep};
1010
use tracing::{debug, error};
1111

1212
pub enum SessionState {
@@ -32,6 +32,14 @@ impl SessionState {
3232
Self::Disconnected(DisconnectedState::new(reconnect, reason))
3333
}
3434

35+
pub fn new_active(writer: WriterRef, heartbeat_interval: u64) -> Self {
36+
let heartbeat_timer = Box::pin(sleep(Duration::from_secs(heartbeat_interval)));
37+
Self::Active(ActiveState {
38+
writer,
39+
heartbeat_timer,
40+
})
41+
}
42+
3543
pub fn should_reconnect(&self) -> bool {
3644
match self {
3745
SessionState::Disconnected(DisconnectedState { reconnect, .. }) => *reconnect,
@@ -163,8 +171,8 @@ impl SessionState {
163171
}
164172

165173
pub struct ActiveState {
166-
pub(crate) writer: WriterRef,
167-
pub(crate) heartbeat_timer: Pin<Box<Sleep>>,
174+
writer: WriterRef,
175+
heartbeat_timer: Pin<Box<Sleep>>,
168176
}
169177

170178
/// Session state we're in while processing messages we requested to be resent.

0 commit comments

Comments
 (0)