sessions

package
v0.1.45 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: Apache-2.0 Imports: 39 Imported by: 0

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 added in v0.1.38

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.

Types

type Action added in v0.1.38

type Action string

Action is what a caller does with a session: read it, or control it.

const (
	// ActionRead lists a session, or serves its info, events or result.
	ActionRead Action = "read"
	// ActionControl starts, stops, extends or restarts a session.
	ActionControl Action = "control"
)

type AuthorizeFunc added in v0.1.38

type AuthorizeFunc func(r *http.Request, profile string, action Action) error

AuthorizeFunc decides whether r may perform action on a session of profile. A non-nil error refuses. On a route naming a session id, a caller who may not read the session is answered 404 exactly as for a session that does not exist; a caller who may read it but not control it gets 403 with the error. Where the caller named the profile itself, a refusal is 403 with the error.

type ContextProvider

type ContextProvider func() dbcontext.Context

type EventRecords added in v0.1.38

type EventRecords interface {
	Meta(ctx context.Context, stream string) (recordstore.Meta, error)
	Scan(ctx context.Context, stream string, afterSeq int64, fn func(seq int64, row recordstore.Row) error) error
	Tail(ctx context.Context, stream string, afterSeq int64, fn func(seq int64, row recordstore.Row) error) error
}

EventRecords reads the record streams a session's status.events names: recordstore.Notifier is one.

type KVResolver added in v0.1.38

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 added in v0.1.38

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 added in v0.1.38

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 added in v0.1.38

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

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

func (*KVStore) Begin added in v0.1.38

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

func (*KVStore) Delete added in v0.1.38

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 added in v0.1.38

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

func (*KVStore) Lineage added in v0.1.38

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 added in v0.1.38

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 added in v0.1.38

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 added in v0.1.38

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

KVStoreOptions configures a KVStore.

type Options

type Options struct {
	Profiles ProfileStoreProvider
	Context  ContextProvider
	Registry *query.SessionRegistry

	// Store answers for sessions that are not live in the registry: the list,
	// info, lineage, and the records restart and replay start from. Nil lists
	// only the registry's sessions.
	Store query.SessionStore

	// EventLog replays the events of a stream session that recorded no
	// status.events, after it left the registry.
	EventLog SessionEventLog

	// Records replays a session's status.events after its owner process is
	// gone, and follows it while this process still writes it.
	Records EventRecords

	// Authorize decides whether r may read or control a session of the named
	// profile. It is asked before a session starts, stops, extends or restarts
	// (ActionControl) and before a session's info, events or result is served
	// (ActionRead): routes that name no profile, so a gate on profile paths
	// alone cannot cover them. A session the caller may not read is answered
	// 404, as a missing one is, so its id never confirms it exists; any other
	// refusal is answered 403 with its error. The list, its totals, its
	// lookups and restartedAs leave out refused profiles. Nil allows
	// every caller everything, which is right only when every profile served
	// has the same permission. A connection trace session is named by its
	// virtual profile, connection-<id>-sql-xevent.
	Authorize AuthorizeFunc

	// Principal names the caller of r, for a stop reason and the principal of
	// a restarted session. Nil stops "via API" and restarts as the previous
	// principal.
	Principal func(r *http.Request) string
}

type ProfileStoreProvider

type ProfileStoreProvider func() (profilestore.Store, error)

type Runner

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

func NewRunner

func NewRunner(options RunnerOptions) (*Runner, error)

func (*Runner) RunTop

func (r *Runner) RunTop(ctx context.Context, name string, options TopOptions) error

func (*Runner) RunTrace

func (r *Runner) RunTrace(ctx context.Context, name string, options TraceOptions) error

type RunnerOptions

type RunnerOptions struct {
	Profiles ProfileStoreProvider
	Context  ContextProvider
	Stdout   io.Writer
	Stderr   io.Writer

	// BeforeExecute, when set, prepares a CLI session's data as it starts and
	// before every later sample — the hook the profile service runs, so a
	// session reads what a one-off run of the profile would.
	BeforeExecute profilestore.BeforeExecuteFunc
}

type Service

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

func New

func New(options Options) (*Service, error)

func (*Service) Handler

func (s *Service) Handler(prefix string, next http.Handler) (http.Handler, error)

type SessionEventLog added in v0.1.38

type SessionEventLog interface {
	Events(ctx context.Context, id string) ([]query.Event, error)

	// HasEvents reports whether the log holds any event of session id, which
	// is what a session's eventsAvailable says without reading them all.
	HasEvents(ctx context.Context, id string) (bool, error)
}

SessionEventLog holds the events a capture stream session emitted through the registry's EventSink, for replay after the session left the registry.

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 added in v0.1.38

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 added in v0.1.38

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 added in v0.1.41

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 added in v0.1.38

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 added in v0.1.38

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 added in v0.1.38

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.

type TopOptions

type TopOptions struct {
	Interval string
	Duration string
	Params   []string
}

type TraceOptions

type TraceOptions struct {
	Duration string
	Params   []string
	Output   string
}

Jump to

Keyboard shortcuts

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