Skip to content

Commit cbfb92b

Browse files
committed
Push state transition decision for logout flow down to the state
1 parent 5b1367f commit cbfb92b

2 files changed

Lines changed: 22 additions & 21 deletions

File tree

crates/hotfix/src/session.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) use crate::session::session_ref::InternalSessionRef;
4848
pub use crate::session::session_ref::InternalSessionRef;
4949
use crate::session::session_ref::OutboundRequest;
5050
use crate::session::state::SessionState;
51-
use crate::session::state::{AwaitingLogonState, AwaitingLogoutState, TestRequestId};
51+
use crate::session::state::{AwaitingLogonState, TestRequestId};
5252
use crate::session_schedule::{SessionPeriodComparison, SessionSchedule};
5353
use crate::store::MessageStore;
5454
use crate::transport::writer::WriterRef;
@@ -368,26 +368,9 @@ where
368368
.on_logout("peer has logged us out")
369369
.await;
370370

371-
match self.state {
372-
// if the session is already disconnected, we have nothing else to do
373-
SessionState::Disconnected(..) => {}
374-
// if we initiated the logout, preserve the reconnect flag
375-
SessionState::AwaitingLogout(AwaitingLogoutState { reconnect, .. }) => {
376-
self.state.disconnect_writer().await;
377-
self.apply_transition(TransitionResult::TransitionTo(
378-
SessionState::new_disconnected(reconnect, "logout completed"),
379-
))
380-
.await;
381-
}
382-
// otherwise assume it makes sense to try to reconnect
383-
_ => {
384-
self.state.disconnect_writer().await;
385-
self.apply_transition(TransitionResult::TransitionTo(
386-
SessionState::new_disconnected(true, "peer has logged us out"),
387-
))
388-
.await;
389-
}
390-
}
371+
self.state.disconnect_writer().await;
372+
let transition = self.state.on_peer_logout();
373+
self.apply_transition(transition).await;
391374

392375
self.ctx.store.increment_target_seq_number().await?;
393376
Ok(())

crates/hotfix/src/session/state.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ impl SessionState {
6363
})
6464
}
6565

66+
/// Returns the transition to apply when a Logout message is received from the peer.
67+
/// Each state determines its own reconnect policy and disconnect reason.
68+
pub(crate) fn on_peer_logout(&self) -> TransitionResult {
69+
match self {
70+
Self::AwaitingLogout(AwaitingLogoutState { reconnect, .. }) => {
71+
TransitionResult::TransitionTo(Self::new_disconnected(
72+
*reconnect,
73+
"logout completed",
74+
))
75+
}
76+
Self::Disconnected(_) => TransitionResult::Stay,
77+
_ => TransitionResult::TransitionTo(Self::new_disconnected(
78+
true,
79+
"peer has logged us out",
80+
)),
81+
}
82+
}
83+
6684
/// Returns the transition to apply when the transport layer reports a disconnect.
6785
/// Each state determines its own reconnect policy.
6886
pub(crate) fn on_disconnect(&self, reason: &str) -> TransitionResult {

0 commit comments

Comments
 (0)