缓冲的(Buffered)会话(Session)服务(Service)

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())

函数

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

追加(数据) 事件 给……;发送到…… session将该状态变化(即 “状态增量”)应用到内存中的会话数据中,并记录这一事件。 temp: 这些键会被应用到内存中的状态数据上(这样后续参与调用的代理程序就可以读取这些数据),但之后会从内存中删除这些键。 事件 在数据被持久化(即存储到数据库中)之前。

Link copied to clipboard
open override fun close()

在每次会话结束时都会清除所有数据并停止写入操作,但不会关闭相关文件或连接。 **delegate**; 好像 flush 使用(with a) null 当使用 `key` 时,会抛出一个异常(错误)。 SessionException 对于那些发生故障的情况,没有任何会话报告相关信息。该函数会阻塞调用它的线程;因此,请不要在 Android 的主线程或需要执行写入操作的线程上调用它。

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

关闭会话。

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

使用指定的参数创建一个新的会话。

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

等待会话中已缓冲的写入操作完成(因为这些操作尚未被实际写入磁盘)。 app: 和 user: 当这些更改(或修改)仍然有效(即未被后续的更新所取代)时,就从系统中删除它们。 **delegate**. 喜欢 flush如果某个操作失败了,或者失败的情况没有被报告,系统会抛出异常(即抛出错误),这样调用者就可以重新尝试该操作。请先停止追加数据:在删除操作开始之后再进行的数据追加操作不会被系统等待;如果这些数据在删除操作之后才被写入存储系统,存储系统可能会拒绝接收这些数据,从而导致操作失败。 flush reports.

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

等待那些已被缓冲的写入操作被执行。 key或者,在每次会话开始时(即每次用户登录时)…… key 是 null并将该操作(“flush”)传递给下一个相关组件/过程。 **delegate**如果写入操作失败,则会抛出异常。 key 它等待那些失败或……(此处“……”应为原文中的缺失内容)。 key 存在一处未报告的故障。 null key在每次调用过程中,系统会抛出所有未被记录的错误;只有当某个会话中的写入操作失败时(无论是通过 `append`、`flush` 还是 `delete` 操作),这些错误才会被记录下来。

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

检索特定的会话(session),并可选择性地过滤其中包含的事件(events)。

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

列出特定会话中的所有事件。

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

列出与特定应用程序和用户关联的会话(session)。