sessionstore

package
v0.1.99 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 Imports: 17 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const DefaultSessionTTL = 30 * 24 * time.Hour

DefaultSessionTTL is how long a kv session record lives from its start.

Variables

This section is empty.

Functions

func KVOrphanedRecords

func KVOrphanedRecords() int64

KVOrphanedRecords counts the start records any KVStore in this process found without their status — evicted under maxmemory, say — and removed, with their index entries, rather than failing every list that reached them.

func LineageOf

func LineageOf(records []query.SessionRecord, ids []string) map[string][]string

LineageOf maps each of ids to the records restarted from it, in records' order.

func WithReadMemo

func WithReadMemo(ctx context.Context) context.Context

WithReadMemo returns ctx carrying a fresh read memo. A handler scopes it to one resolution: a record read again later, such as a stream's final status, must be read fresh.

Types

type KVResolver

type KVResolver func(ctx context.Context) (cache.Store, string, error)

KVResolver names the cache a request's session records live in and the key prefix that isolates its environment (e.g. "oipa.lab:").

type KVStore

type KVStore struct {
	// contains filtered or unexported fields
}

KVStore is a query.SessionStore over a clicky cache.Store:

<prefix>sessions:v1:<id>:start   immutable SessionStart JSON
<prefix>sessions:v1:<id>:status  SessionStatus JSON, expiry pinned to start
<prefix>sessions:v1:index        ZSET id → startedAt (unix ms)

func NewKVStore

func NewKVStore(resolve KVResolver, options KVStoreOptions) (*KVStore, error)

NewKVStore stores records in the cache resolve names for each call; the cache is shared with every process using it.

func NewMemoryKVStore

func NewMemoryKVStore(options KVStoreOptions) (*KVStore, error)

NewMemoryKVStore stores records in this process's memory for its lifetime.

func (*KVStore) Begin

func (s *KVStore) Begin(ctx context.Context, rec query.SessionRecord) error

func (*KVStore) Delete

func (s *KVStore) Delete(ctx context.Context, id string) error

Delete removes a persisted session's start, status, and list index entry.

func (*KVStore) Get

func (s *KVStore) Get(ctx context.Context, id string) (query.SessionRecord, bool, error)

func (*KVStore) Lineage

func (s *KVStore) Lineage(ctx context.Context, ids []string) (map[string][]string, error)

Lineage reads only the records that could name ids: those started no earlier than the earliest of them, since a restart always starts after what it restarts.

func (*KVStore) List

func (s *KVStore) List(ctx context.Context, filter query.SessionFilter) (query.SessionPage, error)

List reads every record in the filter's startedAt window (bounded by the ttl), dropping index entries whose start expired, then filters in Go.

func (*KVStore) Update

func (s *KVStore) Update(ctx context.Context, id string, status query.SessionStatus) error

Update overwrites the status with the expiry its start record has left.

type KVStoreOptions

type KVStoreOptions struct {
	// TTL is how long a record lives from its StartedAt; zero is
	// DefaultSessionTTL.
	TTL time.Duration
}

KVStoreOptions configures a KVStore.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the database-backed query.SessionStore and query.EventSink of the query server: session records in the sessions table, and the events of capture stream sessions batch-written to session_events, which is what a session's events are replayed from after it left the registry.

func NewStore

func NewStore(db *gorm.DB, retention time.Duration) (*Store, error)

NewStore creates the store and starts its background flusher.

func (*Store) Append

func (s *Store) Append(_ context.Context, e query.Event) error

Append buffers the event, flushing synchronously once a full batch has accumulated. It returns the failure of an earlier background flush of the session, so the session emitting it fails.

func (*Store) Begin

func (s *Store) Begin(ctx context.Context, rec query.SessionRecord) error

func (*Store) BindResolver

func (s *Store) BindResolver(resolve func(id string) (*query.Session, bool))

BindResolver wires the live-session lookup used to fail sessions whose events cannot be persisted by the background flusher.

func (*Store) Close

func (s *Store) Close() error

Close stops the background flusher after a final flush.

func (*Store) CloseSession

func (s *Store) CloseSession(_ context.Context, id string) error

CloseSession writes whatever the session still has buffered, along with any failure the background flusher recorded for it while it had no live handle. The registry calls it as the session ends, before its terminal status is written, so the record a reader finds is never ahead of the events table.

func (*Store) Events

func (s *Store) Events(ctx context.Context, id string) ([]query.Event, error)

Events returns the persisted events for a session, in sequence order.

func (*Store) Flush

func (s *Store) Flush() error

Flush writes all buffered events, joining any errors (including flush failures recorded for sessions with no live handle).

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (query.SessionRecord, bool, error)

func (*Store) HasEvents

func (s *Store) HasEvents(ctx context.Context, id string) (bool, error)

HasEvents reports whether any event of session id is buffered or persisted.

func (*Store) Lineage

func (s *Store) Lineage(ctx context.Context, ids []string) (map[string][]string, error)

func (*Store) List

func (s *Store) List(ctx context.Context, filter query.SessionFilter) (query.SessionPage, error)

List filters the scalar fields and the window in SQL, and authorizes by narrowing to the allowed profiles among those that match. Without a label filter SQL also counts, sorts and pages; labels live in the start JSON, so with one the matches are paged in Go.

func (*Store) Prune

func (s *Store) Prune(ctx context.Context) error

Prune atomically deletes sessions stopped before the retention window and their events.

func (*Store) Update

func (s *Store) Update(ctx context.Context, id string, status query.SessionStatus) error

Update writes the status. The registry settles the session's events through Close before writing a terminal status, so the record never ends ahead of its events.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL