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
impl Session
Sourcepub fn scoped<K: 'static, T: Default + Send + Sync + 'static>(
self: &Arc<Self>,
) -> Arc<T> ⓘ
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, _>()
}Sourcepub fn id(&self) -> u64
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.
Sourcepub fn stopped(&self) -> bool
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.
Sourcepub async fn on_stop(&self)
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.
Sourcepub fn is_live(&self) -> bool
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?”.
Sourcepub fn chat_state(&self) -> Arc<Mutex<ChatState>> ⓘ
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.
Sourcepub fn acquire_read(self: &Arc<Self>) -> Result<ConnectionGuard, String>
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.
Sourcepub fn acquire_write(self: &Arc<Self>) -> Result<WriteConnectionGuard, String>
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§
Auto Trait Implementations§
impl !Freeze for Session
impl !RefUnwindSafe for Session
impl !UnwindSafe for Session
impl Send for Session
impl Sync for Session
impl Unpin for Session
impl UnsafeUnpin for Session
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
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> 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