BufferedSessionService

class BufferedSessionService @JvmOverloads constructor(delegate: SessionService, context: CoroutineContext, config: BufferedSessionServiceConfig = BufferedSessionServiceConfig()) : SessionService, AutoCloseable

A SessionService that persists writes in the background: appendEvent updates the live session and returns, and a writer per session writes the events to delegate in order, and in order across sessions for app: and user: state, retrying as BufferedSessionServiceConfig.retryPolicy decides.

Buffered writes stay in memory until they are persisted; reads that go to the store and flush wait for the writes they depend on, and the runner flushes before each final response, which bounds how many writes a session holds. When retries run out, the session's buffered writes are dropped and a SessionException reports the failure to any flush or delete waiting on them, or else to the session's next append, flush or delete unless failures in 1024 other sessions displaced it; reload the session with getSession before appending again, since the live session still holds the dropped events.

Parameters

delegate

The store the writes are persisted to, which close leaves open; its SessionService.appendEvent receives a Session with only the key and the Session.lastUpdateTime the write replaces, so it must persist from the event alone. A retry after an attempt that failed but was persisted resends the event with that same Session.lastUpdateTime, so the store should treat an event it already holds as persisted, rather than store it twice or, with a stale-write check, reject it and fail the session. A call that never returns holds back the session's later writes, the reads that wait for them, and close.

context

Where the writes run. They outlive the run that made them, so they take their own context rather than the caller's; a kotlinx.coroutines.Job in it becomes their parent and cannot complete until close. An Error in a write stops the service and fails like an uncaught child coroutine, canceling a parent job that is not a supervisor and reaching the context's exception handler or the platform default.

config

How failed writes are retried, and whether a read cache serves getSession.

Constructors

Link copied to clipboard
constructor(delegate: SessionService, context: CoroutineContext, config: BufferedSessionServiceConfig = BufferedSessionServiceConfig())

Functions

Link copied to clipboard
open suspend override fun appendEvent(session: Session, event: Event): Event

Appends event to session, applying its state delta to the in-memory session and recording the event; temp: keys are applied to the in-memory state (so later agents in the invocation can read them) but removed from event before persistence.

Link copied to clipboard
open override fun close()

Flushes every session and stops the writes, without closing delegate; like flush with a null key, it throws a SessionException for failures no session has reported. It blocks the calling thread, so do not call it on the Android main thread or on a thread the writes need.

Link copied to clipboard
open suspend override fun closeSession(session: Session)

Closes a session.

Link copied to clipboard
open suspend override fun createSession(key: SessionKey, state: Map<String, Any>? = null): Session

Creates a new session with the specified parameters.

Link copied to clipboard
open suspend override fun deleteSession(key: SessionKey)

Waits for the session's buffered writes, since its app: and user: changes outlive it, then deletes it from delegate. Like flush, it throws instead if one failed or a failure is unreported, so the caller can retry. Stop appending first: an append made after the delete starts is not waited for, and if it reaches the store after the delete the store may reject it, leaving a failure that flush reports.

Link copied to clipboard
open suspend override fun flush(key: SessionKey? = null)

Waits for the writes buffered for key, or for every session when key is null, and passes the flush on to delegate, then throws if a write of key it waited on failed or key has a failure unreported. With a null key, it throws every unreported failure, on each call until the session whose write failed reports it on an append, flush or delete.

Link copied to clipboard
open suspend override fun getSession(key: SessionKey, config: GetSessionConfig? = null): Session?

Retrieves a specific session, optionally filtering the events included.

Link copied to clipboard
open suspend override fun listEvents(key: SessionKey): ListEventsResponse

Lists the events within a specific session.

Link copied to clipboard
open suspend override fun listSessions(appName: String, userId: String): ListSessionsResponse

Lists sessions associated with a specific application and user.