pub struct SessionStore { /* private fields */ }Expand description
Thread-safe storage for sessions.
Implementations§
Source§impl SessionStore
impl SessionStore
Sourcepub fn create(&self) -> Result<SessionId>
pub fn create(&self) -> Result<SessionId>
Create a new session.
Returns the newly assigned session ID.
Sourcepub fn get(&self, id: &SessionId) -> Result<Option<Session>>
pub fn get(&self, id: &SessionId) -> Result<Option<Session>>
Get a clone of the session with the given ID.
Sourcepub fn update<F>(&self, id: &SessionId, f: F) -> Result<()>
pub fn update<F>(&self, id: &SessionId, f: F) -> Result<()>
Update a session using a closure.
The closure receives a mutable reference to the session and can modify it. Returns an error if the session doesn’t exist.
Sourcepub fn remove(&self, id: &SessionId) -> Result<Option<Session>>
pub fn remove(&self, id: &SessionId) -> Result<Option<Session>>
Remove a session from the store.
Returns the removed session, or None if it didn’t exist.
Sourcepub fn remove_matching<F>(&self, predicate: F) -> Result<usize>
pub fn remove_matching<F>(&self, predicate: F) -> Result<usize>
Remove all sessions matching a predicate.
Returns the number of sessions removed.
Sourcepub fn sweep_idle(&self, ttl: Duration) -> Result<Vec<SessionId>>
pub fn sweep_idle(&self, ttl: Duration) -> Result<Vec<SessionId>>
Drop sessions that have sat idle past ttl, returning their ids.
Nothing reclaimed a shell session before this: there was no TTL, no cap
and no sweeper, and remove_matching had no
caller outside tests. A client that creates sessions and never DELETEs
them therefore accumulated them for as long as the process ran. Upload
sessions have had exactly this — a periodic sweep against
fs::SESSION_TTL — since they were introduced; shell sessions simply had
no equivalent.
A session running a command is never swept, whatever its idle clock
says. That clock is only advanced when a command starts and when it
ends (BusySession), so during a long command it does not move at all —
idle time alone cannot tell “abandoned” from “busy”, and a sweep keyed on
it would reap a session with a command actively running in it. The state
is what distinguishes them: the same guard that stops the clock also
holds the session Active for the length of the
command, and that guard closes every exit including a cancelled future.
Active cannot hide an unbounded session either, since a command’s
deadline is bounded (execution::MAX_TIMEOUT).
That last sentence is only true while the guard’s life is the command’s
life, which is a stronger condition than it sounds. Until 0.21.1 the
session WebSocket handler held it across delivery as well, and delivery
is bounded by nothing: a consumer that stopped reading its socket parked
the handler mid-send, so the session stayed Active — and unsweepable —
for as long as that consumer liked. Measured at 75 s on a command that
died at its 5 s deadline, ending when the consumer resumed rather than at
any deadline at all. A guard that outlives what it claims to track puts
this sweep back where it was before there was one.
Ids are returned rather than counted so the caller can record what went; a session vanishing with no trace is what makes an abandoned one indistinguishable from one the client deleted.