pub struct SessionSet<K, S> { /* private fields */ }Expand description
A keyed collection of sessions driven as one.
K is whatever identifies a peer to the application: a node id, a serial number, a
database row id. The set never interprets it.
Implementations§
Source§impl<K: Ord, S: Session> SessionSet<K, S>
impl<K: Ord, S: Session> SessionSet<K, S>
Sourcepub fn insert(&mut self, key: K, session: S) -> Option<S>
pub fn insert(&mut self, key: K, session: S) -> Option<S>
Add a session, returning whatever was under that key.
Sourcepub fn remove(&mut self, key: &K) -> Option<S>
pub fn remove(&mut self, key: &K) -> Option<S>
Take a session out — when its transport has gone, for instance.
Sourcepub fn get_mut(&mut self, key: &K) -> Option<&mut S>
pub fn get_mut(&mut self, key: &K) -> Option<&mut S>
One session, mutably, to feed a frame into.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut S)>
pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut S)>
Every session, mutably, in key order.
Sourcepub fn poll_timeout(&self) -> Option<Timestamp>
pub fn poll_timeout(&self) -> Option<Timestamp>
The soonest deadline across the whole set.
This is the number an event loop sleeps on. A set of a thousand sessions still has exactly one timer.
Sourcepub fn next_deadline(&self) -> Option<(&K, Timestamp)>
pub fn next_deadline(&self) -> Option<(&K, Timestamp)>
Which session owns the soonest deadline, for a caller that would rather wake one session than sweep them all.
Sourcepub fn handle_timeout(&mut self, now: Timestamp)
pub fn handle_timeout(&mut self, now: Timestamp)
Fire every deadline that has come due.
Sweeping the whole set rather than only the session that woke us is deliberate: several deadlines commonly fall in the same millisecond, and a loop that handles one per wake-up turns a burst into a long tail of timer round-trips.
Sourcepub fn drain_transmit(&mut self) -> Vec<(K, Outgoing)>where
K: Clone,
pub fn drain_transmit(&mut self) -> Vec<(K, Outgoing)>where
K: Clone,
Everything every session wants to send, tagged with whose it is.
The key is cloned, not borrowed. Draining and then acting on what was drained is the only thing a caller does with the result — write the frame to the socket it looks up by that key, close the session that just emitted it, remove one whose transport has gone — and a borrowed key would hold the set immutably for exactly as long as the result lives.
Sourcepub fn drain_events(&mut self) -> Vec<(K, S::Event)>where
K: Clone,
pub fn drain_events(&mut self) -> Vec<(K, S::Event)>where
K: Clone,
Everything every session wants to tell the application, tagged with whose it is.
Cloned for the same reason drain_transmit clones: acting
on an event usually means touching the set the event came from.
Sourcepub fn retain_open(&mut self) -> usize
pub fn retain_open(&mut self) -> usize
Drop every session whose state machine has finished.
A set that is never pruned is a memory leak with extra steps: a reconnecting device produces a new session each time, and the closed ones have nothing left to say.