session

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package session manages persistent shell sessions for the memsh HTTP server. Each session has its own virtual filesystem (afero.MemMapFs) and working directory that persist across HTTP requests when using the X-Session-ID header.

Sessions support:

  • Virtual filesystem isolation between sessions
  • Working directory persistence
  • Automatic alias persistence
  • Cron job scheduling per session
  • TTL-based expiration and reaping

Index

Constants

View Source
const AliasesFile = "/.memsh_session_aliases"

AliasesFile is the virtual-FS path used to persist aliases across requests.

Variables

This section is empty.

Functions

func LoadShellSession added in v0.0.13

func LoadShellSession(dir, id string) (afero.Fs, string, time.Duration, bool, network.Usage, error)

LoadShellSession restores one persisted shell session by id.

func RestoreAliases

func RestoreAliases(ctx context.Context, sh *shell.Shell, fs afero.Fs)

RestoreAliases sources AliasesFile if it exists.

func SaveAliases

func SaveAliases(ctx context.Context, sh *shell.Shell)

SaveAliases writes the current alias table to AliasesFile in the virtual FS so the next shell can restore it via source.

func SaveShellSession added in v0.0.13

func SaveShellSession(dir, id string, fs afero.Fs, cwd string, runtime time.Duration, rcLoaded bool, netUsage network.Usage) error

SaveShellSession saves one shell session to persistence storage.

func StartScheduler

func StartScheduler(ctx context.Context, store *Store, baseOpts []shell.Option, timeout time.Duration)

StartScheduler fires cron jobs for every active session once per minute. It aligns to the next minute boundary before starting the ticker so that CronMatches is evaluated at a consistent wall-clock minute. ctx should be cancelled when the server shuts down.

Types

type Entry

type Entry struct {
	Fs        afero.Fs
	Cwd       string
	CreatedAt time.Time
	LastUse   time.Time
	RcLoaded  bool // true after /.memshrc has been sourced for this session
	Runtime   time.Duration
	Network   network.Usage
	CronMu    sync.Mutex // serialises concurrent cron job writes to /.cron_log
}

Entry holds the persistent state (filesystem + cwd) shared across requests. Each request still creates its own shell.Shell pointing at the session's FS, so per-request I/O capture works correctly.

type Info

type Info struct {
	ID        string `json:"id"`
	Cwd       string `json:"cwd"`
	CreatedAt string `json:"created_at"`
	LastUse   string `json:"last_use"`
	RuntimeMS int64  `json:"runtime_ms"`
	NetReqs   int    `json:"network_requests"`
	NetSent   int64  `json:"network_bytes_sent"`
	NetRecv   int64  `json:"network_bytes_received"`
	NetRunMS  int64  `json:"network_runtime_ms"`
}

Info is one entry in the session list response.

type Limits added in v0.0.13

type Limits struct {
	MaxFiles   int
	MaxBytes   int64
	MaxRuntime time.Duration
}

Limits defines optional per-session resource caps. A zero value for each field means "unlimited" for that resource.

func (Limits) EffectiveTimeout added in v0.0.13

func (l Limits) EffectiveTimeout(base, used, minTimeout time.Duration) (time.Duration, error)

func (Limits) HasFSLimits added in v0.0.13

func (l Limits) HasFSLimits() bool

func (Limits) ValidateFS added in v0.0.13

func (l Limits) ValidateFS(fs afero.Fs) error

func (Limits) ValidateRuntime added in v0.0.13

func (l Limits) ValidateRuntime(used time.Duration) error

type Snap

type Snap struct {
	ID     string
	Fs     afero.Fs
	Cwd    string
	CronMu *sync.Mutex
}

Snap is a lightweight copy of a session's key fields for use by the cron scheduler without holding the store lock during job execution.

type Store

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

Store manages persistent shell sessions keyed by an arbitrary ID.

func New

func New(ctx context.Context, ttl time.Duration, maxEntries int) *Store

New creates a new session store with the given TTL and max entries. The background reaper goroutine runs until ctx is cancelled, so callers should pass a context tied to the server lifetime to avoid goroutine leaks on shutdown.

func NewPersistent added in v0.0.13

func NewPersistent(ctx context.Context, ttl time.Duration, maxEntries int, persistDir string) (*Store, error)

NewPersistent creates a new session store with durable persistence.

func (*Store) Count

func (st *Store) Count() int

Count returns the number of active sessions.

func (*Store) Delete

func (st *Store) Delete(id string)

Delete removes and discards a session.

func (*Store) Get

func (st *Store) Get(id string) (*Entry, bool)

Get returns an existing session entry or creates one. Returns (entry, true) on success, or (nil, false) if the max session limit would be exceeded by creating a new session.

func (*Store) List

func (st *Store) List() []Info

List returns all sessions sorted by last use (most recent first).

func (*Store) Replace

func (st *Store) Replace(id string, fs afero.Fs, cwd string)

Replace creates or overwrites a session with the given filesystem and cwd. Used by the snapshot import endpoint.

func (*Store) Snapshot

func (st *Store) Snapshot() []Snap

Snapshot returns a point-in-time copy of all active sessions.

func (*Store) Update

func (st *Store) Update(id, cwd string, rcLoaded bool)

Update records the cwd after a request finishes so the next request in the same session picks it up. Alias state is persisted in the virtual FS via /.memsh_session_aliases, so it does not need a separate field here.

func (*Store) UpdateWithRuntime added in v0.0.13

func (st *Store) UpdateWithRuntime(id, cwd string, rcLoaded bool, runtimeDelta time.Duration)

UpdateWithRuntime updates session state and runtime accounting.

func (*Store) UpdateWithRuntimeAndNetwork added in v0.0.13

func (st *Store) UpdateWithRuntimeAndNetwork(id, cwd string, rcLoaded bool, runtimeDelta time.Duration, netUsage network.Usage)

UpdateWithRuntimeAndNetwork updates session state and cumulative network usage.

Jump to

Keyboard shortcuts

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