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
- func LoadShellSession(dir, id string) (afero.Fs, string, time.Duration, bool, network.Usage, error)
- func RestoreAliases(ctx context.Context, sh *shell.Shell, fs afero.Fs)
- func SaveAliases(ctx context.Context, sh *shell.Shell)
- func SaveShellSession(dir, id string, fs afero.Fs, cwd string, runtime time.Duration, rcLoaded bool, ...) error
- func StartScheduler(ctx context.Context, store *Store, baseOpts []shell.Option, ...)
- type Entry
- type Info
- type Limits
- type Snap
- type Store
- func (st *Store) Count() int
- func (st *Store) Delete(id string)
- func (st *Store) Get(id string) (*Entry, bool)
- func (st *Store) List() []Info
- func (st *Store) Replace(id string, fs afero.Fs, cwd string)
- func (st *Store) Snapshot() []Snap
- func (st *Store) Update(id, cwd string, rcLoaded bool)
- func (st *Store) UpdateWithRuntime(id, cwd string, rcLoaded bool, runtimeDelta time.Duration)
- func (st *Store) UpdateWithRuntimeAndNetwork(id, cwd string, rcLoaded bool, runtimeDelta time.Duration, ...)
Constants ¶
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
LoadShellSession restores one persisted shell session by id.
func RestoreAliases ¶
RestoreAliases sources AliasesFile if it exists.
func SaveAliases ¶
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
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 (Limits) HasFSLimits ¶ added in v0.0.13
type Snap ¶
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 ¶
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) Get ¶
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) Replace ¶
Replace creates or overwrites a session with the given filesystem and cwd. Used by the snapshot import endpoint.
func (*Store) Update ¶
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
UpdateWithRuntime updates session state and runtime accounting.