Skip to main content

EventLoop

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,
}
Available on crate feature 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: bool

false 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: XorShift32

Seeded 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: u32

Consecutive 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>

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>

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)

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>

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)

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, )

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>

pub(crate) fn new( transport: T, options: ClientOptions, clean_start: bool, session: JournaledSession, store: Persistence, endpoints: Endpoints, ) -> 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<()>

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

Runs one connection from the handshake to the teardown.

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>

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more