session

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2026 License: MIT Imports: 10 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 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 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
	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"`
}

Info is one entry in the session list response.

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(ttl time.Duration, maxEntries int) *Store

New creates a new session store with the given TTL and max entries.

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.

Jump to

Keyboard shortcuts

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