Skip to main content

Session

Struct Session 

Source
pub struct Session { /* private fields */ }
Expand description

One account’s database resources, held behind an Arc.

The connections an account uses are reachable ONLY through its own Session, so a task that captured one keeps talking to the account it started with even after a swap — it cannot be handed the next account’s database, because it never asks “who is current?” again.

That makes teardown structural: swapping installs a new Session and drops the reference to the old one, whose pool closes when the last in-flight guard finishes with it. Nothing to clear, nothing to remember to clear.

Implementations§

Source§

impl Session

Source

pub fn scoped<K: 'static, T: Default + Send + Sync + 'static>( self: &Arc<Self>, ) -> Arc<T>

This account’s instance of T, built on first touch.

The home for anything an account owns beyond its database: caches keyed by its row ids, queues holding its work, routing tables holding its keys. Previously these were process globals with a hand-written clear in two teardown paths, which is how they drifted — one path cleared fields the other did not, and a cache added without a clear leaked into the next account. Here there is nothing to clear: the account’s instances go when its session does.

Keyed by a marker type the owning module declares, so the session never has to know what any of this is, and two modules storing the same SHAPE (say a Mutex<HashMap<PublicKey, _>>) can’t collide on one slot.

struct InboxRelayCache;               // the key, private to this module
fn cache() -> Arc<Mutex<HashMap<PublicKey, CachedRelays>>> {
    db::current_session().scoped::<InboxRelayCache, _>()
}
Source

pub fn id(&self) -> u64

This session’s identity. Stable across a re-initialise of the same account, distinct for every other. Callers that key a cache by “which account is this” use it in place of the old generation counter.

Source

pub fn stopped(&self) -> bool

Whether this account has been switched away from.

Purely an efficiency signal. Work that continues past it is still CORRECT — it writes to this account’s own storage and paints nothing — it is just no longer work anyone is waiting for. Long syncs poll this at their loop heads so a swap stops the relay traffic and the decryption rather than grinding on for a screen nobody is looking at.

Never use it to decide whether a write is SAFE. That is structural now.

Source

pub async fn on_stop(&self)

Resolves once this account is switched away from — for select! against a fetch, so an in-flight request is dropped instead of awaited out.

Source

pub fn is_live(&self) -> bool

Whether this session is the account currently on screen.

For code holding a session it captured earlier — a drop handler, a callback fired by a relay OK — where there is no await to be bound across and the question is “is what I captured still current?”.

Source

pub fn chat_state(&self) -> Arc<Mutex<ChatState>>

This account’s in-memory chats and profiles.

Reached through the session, so a task bound to account A keeps reading and writing A’s chats after a swap. Its state is then an orphan nothing displays, rather than corruption in the account now on screen.

Source

pub fn acquire_read(self: &Arc<Self>) -> Result<ConnectionGuard, String>

Take a READ connection from this session, opening one against this session’s own database if the pool is empty.

Source

pub fn acquire_write(self: &Arc<Self>) -> Result<WriteConnectionGuard, String>

Take THE write connection from this session, opening one against this session’s own database if the slot is empty.

Trait Implementations§

Source§

impl Debug for Session

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = Infallible

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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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