Struct EventLoop
pub(crate) struct EventLoop<T> {Show 17 fields
transport: T,
options: ClientOptions,
clean_start: bool,
client: Client<JournaledSession>,
store: Persistence,
cmd_rx: Receiver<Command>,
cmds_open: bool,
shutdown_rx: Receiver<()>,
msg_tx: Sender<Message>,
status_tx: Sender<ConnectionStatus>,
queued_commands: VecDeque<Command>,
offline_pending: HashMap<PacketId, PendingPublish>,
offline_acks: HashMap<PacketId, PendingAck>,
queued_waiting: Vec<Sender<Result<(), Error>>>,
backoff_delay: Option<Duration>,
jitter_rng: XorShift32,
reconnect_attempt: u32,
}tokio only.Expand description
The connection event loop. It owns the client state machine and the state
that persists across reconnects, and runs one
Connection at a time.
Fields§
§transport: T§options: ClientOptions§clean_start: bool§client: Client<JournaledSession>§store: Persistence§cmd_rx: Receiver<Command>§cmds_open: boolfalse once every client handle is dropped and cmd_rx is drained.
The selects then stop polling the closed channel, which would otherwise
resolve on every iteration.
shutdown_rx: Receiver<()>§msg_tx: Sender<Message>§status_tx: Sender<ConnectionStatus>§queued_commands: VecDeque<Command>§offline_pending: HashMap<PacketId, PendingPublish>Completion futures for QoS >= 1 publishes accepted while offline.
Their messages are already in the durable outbox. The next
connection adopts them so the eventual acknowledgement resolves
them.
offline_acks: HashMap<PacketId, PendingAck>Completion futures for SUBSCRIBE/UNSUBSCRIBE accepted while offline, or left in flight when a connection dropped. The durable in-flight entries are in the session. The next connection adopts them.
queued_waiting: Vec<Sender<Result<(), Error>>>queued() futures waiting for the persist that covers their session
mutation. Kept across connections like the ops they wait on.
backoff_delay: Option<Duration>The delay before the next connection attempt. None connects at once.
jitter_rng: XorShift32Seeded per client instance. A fixed seed would give every client the same jitter sequence, and a fleet that lost its broker would reconnect at the same moments.
reconnect_attempt: u32Consecutive reconnect attempts, reset once a connection is re-established so each outage starts its log count from one.
Implementations§
§impl<T: Transport + Send + Sync + 'static> EventLoop<T>
impl<T: Transport + Send + Sync + 'static> EventLoop<T>
pub(super) async fn connect(
&mut self,
) -> Result<(Connection<T::Io>, bool), DisconnectReason>
pub(super) async fn connect( &mut self, ) -> Result<(Connection<T::Io>, bool), DisconnectReason>
Connects and handshakes within the connect timeout, so a broker that accepts the socket but never answers cannot hang the client. Returns the connection and whether the broker has our session.
pub(super) async fn resume(
&mut self,
conn: &mut Connection<T::Io>,
session_present: bool,
) -> Result<(), DisconnectReason>
pub(super) async fn resume( &mut self, conn: &mut Connection<T::Io>, session_present: bool, ) -> Result<(), DisconnectReason>
Brings the session in line with the broker’s and puts everything pending on the wire.
pub(super) async fn drive(
&mut self,
conn: &mut Connection<T::Io>,
) -> (DisconnectReason, bool)
pub(super) async fn drive( &mut self, conn: &mut Connection<T::Io>, ) -> (DisconnectReason, bool)
Drives conn until the connection ends. Returns why it ended and
whether an enqueued DISCONNECT should be flushed.
fn on_command(
&mut self,
conn: &mut Connection<T::Io>,
cmd: Option<Command>,
) -> Result<(), Error>
fn on_command( &mut self, conn: &mut Connection<T::Io>, cmd: Option<Command>, ) -> Result<(), Error>
Applies a command, or notes that every client handle is dropped. The session keeps running then. Only the owner handle ends it.
§Errors
Returns the error of handle_command. The connection must then end.
fn apply_command( &mut self, conn: &mut Connection<T::Io>, cmd: Command, ) -> Result<(), Error>
fn on_shutdown(
&mut self,
conn: &mut Connection<T::Io>,
user_disconnect: bool,
) -> (DisconnectReason, bool)
fn on_shutdown( &mut self, conn: &mut Connection<T::Io>, user_disconnect: bool, ) -> (DisconnectReason, bool)
Ends the connection on request of the owner handle. user_disconnect
is true for a stop() call and false if the handle was dropped,
where nobody observes a graceful DISCONNECT.
pub(super) async fn teardown(
&mut self,
conn: &mut Connection<T::Io>,
graceful: bool,
)
pub(super) async fn teardown( &mut self, conn: &mut Connection<T::Io>, graceful: bool, )
Delivers the buffered messages and flushes a DISCONNECT if graceful.
The futures of operations still in flight move over to the next connection.
§impl<T: Transport + Send + Sync + 'static> EventLoop<T>
impl<T: Transport + Send + Sync + 'static> EventLoop<T>
pub(crate) fn new( transport: T, options: ClientOptions, clean_start: bool, session: JournaledSession, store: Persistence, endpoints: Endpoints, ) -> Self
pub(crate) async fn run(self)
pub(crate) async fn run(self)
Connects, reconnects with backoff, and returns once the session ends.
async fn back_off(&mut self, delay: Duration) -> ControlFlow<()>
async fn back_off(&mut self, delay: Duration) -> ControlFlow<()>
Waits out the backoff delay while applying commands to the session,
so queued() futures resolve without a live connection. Breaks on
shutdown.
fn accept_offline(&mut self, cmd: Command)
async fn run_connection(&mut self) -> ConnectionResult
async fn run_connection(&mut self) -> ConnectionResult
Runs one connection from the handshake to the teardown.
fn plan_reconnect(&mut self, result: &ConnectionResult) -> ControlFlow<()>
fn plan_reconnect(&mut self, result: &ConnectionResult) -> ControlFlow<()>
Decides what follows a connection that ended with result: a reconnect
after a backoff delay, or the end of the session.
async fn persist(&mut self) -> Result<(), DisconnectReason>
async fn persist(&mut self) -> Result<(), DisconnectReason>
Persists pending session ops and, on success, resolves the queued()
futures the write covers. On failure both are kept for the retry after
the backoff-driven reconnect.
Auto Trait Implementations§
impl<T> !RefUnwindSafe for EventLoop<T>
impl<T> !UnwindSafe for EventLoop<T>
impl<T> Freeze for EventLoop<T>where
T: Freeze,
impl<T> Send for EventLoop<T>where
T: Send,
impl<T> Sync for EventLoop<T>where
T: Sync,
impl<T> Unpin for EventLoop<T>where
T: Unpin,
impl<T> UnsafeUnpin for EventLoop<T>where
T: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more