engine

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: AGPL-3.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound        = errors.New("sandbox not found")
	ErrNotRunning      = errors.New("sandbox is not running")
	ErrLimitReached    = errors.New("maximum sandbox limit reached")
	ErrPressureTooHigh = errors.New("host under critical memory pressure")
)

Functions

This section is empty.

Types

type Engine

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

Engine orchestrates sandbox lifecycle.

func NewEngine

func NewEngine(rt runtime.Runtime, st store.Store, cfg config.SandboxConfig, s3Cfg config.S3Config, resCfg config.ResourceConfig, logger *slog.Logger) *Engine

NewEngine creates a new Engine.

func (*Engine) CreateSandbox

func (e *Engine) CreateSandbox(ctx context.Context, cfg runtime.SandboxConfig) (*Sandbox, error)

CreateSandbox creates and starts a new sandbox.

func (*Engine) CurrentPressure added in v0.0.6

func (e *Engine) CurrentPressure() PressureEvent

CurrentPressure returns the current pressure event.

func (*Engine) DeleteSnapshot

func (e *Engine) DeleteSnapshot(ctx context.Context, snapshotID string) error

DeleteSnapshot removes a snapshot.

func (*Engine) DestroySandbox

func (e *Engine) DestroySandbox(ctx context.Context, id string) error

DestroySandbox stops and removes a sandbox.

func (*Engine) Exec

func (e *Engine) Exec(ctx context.Context, id string, opts runtime.ExecOpts) (*runtime.ExecResult, error)

Exec runs a command in a sandbox.

func (*Engine) ExecStream

func (e *Engine) ExecStream(ctx context.Context, id string, opts runtime.ExecOpts) (runtime.ExecStream, error)

ExecStream runs a streaming command in a sandbox.

func (*Engine) GetSandbox

func (e *Engine) GetSandbox(id string) (*Sandbox, error)

GetSandbox returns a sandbox by ID.

func (*Engine) ListDir

func (e *Engine) ListDir(ctx context.Context, id string, path string) ([]runtime.FileInfo, error)

ListDir lists a directory in a sandbox.

func (*Engine) ListSandboxes

func (e *Engine) ListSandboxes() []*Sandbox

ListSandboxes returns all tracked sandboxes.

func (*Engine) ListSnapshots

func (e *Engine) ListSnapshots(ctx context.Context, sandboxID string) ([]runtime.SnapshotInfo, error)

ListSnapshots returns all snapshots for a sandbox.

func (*Engine) MkDir

func (e *Engine) MkDir(ctx context.Context, id string, path string) error

MkDir creates a directory in a sandbox.

func (*Engine) PressureThresholds added in v0.0.6

func (e *Engine) PressureThresholds() PressureThresholds

PressureThresholds returns the configured pressure thresholds.

func (*Engine) ReadFile

func (e *Engine) ReadFile(ctx context.Context, id string, path string) ([]byte, error)

ReadFile reads a file from a sandbox.

func (*Engine) RemoveFile

func (e *Engine) RemoveFile(ctx context.Context, id string, path string) error

RemoveFile removes a file from a sandbox.

func (*Engine) RestoreSnapshot

func (e *Engine) RestoreSnapshot(ctx context.Context, snapshotID string) (*Sandbox, error)

RestoreSnapshot restores a sandbox from a snapshot.

func (*Engine) SandboxCount added in v0.0.6

func (e *Engine) SandboxCount() (total, running int)

SandboxCount returns total and running sandbox counts without allocating a slice.

func (*Engine) Shutdown

func (e *Engine) Shutdown(_ context.Context)

Shutdown gracefully stops all sandboxes. Uses a detached context so shutdown is not cancelled by the caller's context.

func (*Engine) Snapshot

func (e *Engine) Snapshot(ctx context.Context, id string, name string) (*runtime.SnapshotInfo, error)

Snapshot creates a snapshot of a sandbox.

func (*Engine) Stats

func (e *Engine) Stats(ctx context.Context, id string) (*runtime.SandboxStats, error)

Stats returns stats for a sandbox.

func (*Engine) StopSandbox

func (e *Engine) StopSandbox(ctx context.Context, id string) error

StopSandbox stops a sandbox without removing it.

func (*Engine) WriteFile

func (e *Engine) WriteFile(ctx context.Context, id string, path string, content []byte) error

WriteFile writes a file to a sandbox.

type LinuxMemoryBackend added in v0.0.6

type LinuxMemoryBackend struct{}

LinuxMemoryBackend reads memory info from /proc/meminfo and cgroup v2 files.

func (*LinuxMemoryBackend) ContainerMemory added in v0.0.6

func (b *LinuxMemoryBackend) ContainerMemory(containerID string) (uint64, error)

func (*LinuxMemoryBackend) HostMemory added in v0.0.6

func (b *LinuxMemoryBackend) HostMemory() (total, used, free uint64, err error)

type MemoryBackend added in v0.0.6

type MemoryBackend interface {
	// HostMemory returns total, used, and free host memory in bytes.
	HostMemory() (total, used, free uint64, err error)

	// ContainerMemory returns memory usage in bytes for a specific container.
	ContainerMemory(containerID string) (uint64, error)
}

MemoryBackend abstracts platform-specific memory reading. Linux uses /proc/meminfo + cgroup v2, macOS uses sysctl + Docker API.

func NewPlatformMemoryBackend added in v0.0.6

func NewPlatformMemoryBackend() MemoryBackend

type PressureEvent added in v0.0.6

type PressureEvent struct {
	Level       PressureLevel
	MemoryUsed  uint64
	MemoryTotal uint64
	Score       float64
	Timestamp   time.Time
}

PressureEvent is emitted when the pressure level changes.

type PressureLevel added in v0.0.6

type PressureLevel int

PressureLevel represents the current host memory pressure.

const (
	PressureNormal    PressureLevel = iota // < 70%
	PressureWarning                        // 70-80%
	PressureHigh                           // 80-90%
	PressureCritical                       // 90-95%
	PressureEmergency                      // > 95%
)

func (PressureLevel) String added in v0.0.6

func (p PressureLevel) String() string

type PressureMonitor added in v0.0.6

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

PressureMonitor observes host memory pressure and notifies listeners. It implements hysteresis: a level change requires 2 consecutive matching readings.

func NewPressureMonitor added in v0.0.6

func NewPressureMonitor(backend MemoryBackend, interval time.Duration, thresholds PressureThresholds, logger *slog.Logger) *PressureMonitor

NewPressureMonitor creates a new PressureMonitor.

func (*PressureMonitor) CurrentEvent added in v0.0.6

func (pm *PressureMonitor) CurrentEvent() PressureEvent

CurrentEvent returns the most recent pressure event.

func (*PressureMonitor) Start added in v0.0.6

func (pm *PressureMonitor) Start()

Start begins the monitoring loop in a goroutine. Safe to call multiple times.

func (*PressureMonitor) Stop added in v0.0.6

func (pm *PressureMonitor) Stop()

Stop signals the monitor to stop and waits for the goroutine to finish. Safe to call multiple times.

func (*PressureMonitor) Subscribe added in v0.0.6

func (pm *PressureMonitor) Subscribe(ch chan<- PressureEvent)

Subscribe registers a channel to receive pressure events. The channel should be buffered to avoid blocking the monitor.

type PressureThresholds added in v0.0.6

type PressureThresholds struct {
	Warning   float64 // default: 0.70
	High      float64 // default: 0.80
	Critical  float64 // default: 0.90
	Emergency float64 // default: 0.95
}

PressureThresholds defines the memory usage thresholds for each pressure level.

func DefaultPressureThresholds added in v0.0.6

func DefaultPressureThresholds() PressureThresholds

DefaultPressureThresholds returns the default thresholds.

type Sandbox

type Sandbox struct {
	ID    string `json:"id"`
	Image string `json:"image"`

	Config    runtime.SandboxConfig `json:"-"`
	CreatedAt time.Time             `json:"created_at"`
	ExpiresAt time.Time             `json:"expires_at,omitempty"`
	Ports     []runtime.PortMapping `json:"ports,omitempty"`
	// contains filtered or unexported fields
}

Sandbox represents a managed sandbox instance.

func (*Sandbox) GetStatus

func (s *Sandbox) GetStatus() runtime.SandboxStatus

GetStatus returns the sandbox status (thread-safe).

func (*Sandbox) IsExpired

func (s *Sandbox) IsExpired() bool

IsExpired returns true if the sandbox has exceeded its timeout.

func (*Sandbox) MarshalJSON

func (s *Sandbox) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling to include the status field.

func (*Sandbox) SetStatus

func (s *Sandbox) SetStatus(status runtime.SandboxStatus)

SetStatus updates the sandbox status (thread-safe).

type WarmPool

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

WarmPool pre-creates containers so sandbox creation is near-instant.

func NewWarmPool

func NewWarmPool(rt runtime.Runtime, size int, cfg runtime.SandboxConfig, logger *slog.Logger) *WarmPool

NewWarmPool creates a warm pool that maintains `size` pre-created containers.

func (*WarmPool) Acquire

func (wp *WarmPool) Acquire() string

Acquire gets a pre-warmed container ID from the pool. Returns empty string if pool is empty (caller should create normally).

func (*WarmPool) Return

func (wp *WarmPool) Return(ctx context.Context, id string)

Return puts an unused container ID back to the pool or removes it.

func (*WarmPool) Size

func (wp *WarmPool) Size() int

Size returns the current number of warm containers.

func (*WarmPool) Stop

func (wp *WarmPool) Stop(ctx context.Context)

Stop drains and destroys all warm containers.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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