shared

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

Overview

Package shared provides common utilities for all examples.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(r *http.Request, v any) error

DecodeJSON decodes a JSON request body.

func ErrorResponse

func ErrorResponse(w http.ResponseWriter, status int, message string)

ErrorResponse writes a JSON error response.

func GetCacheDir

func GetCacheDir() string

GetCacheDir returns a directory for caching filesystem snapshots. Uses CC_CACHE_DIR env var, or falls back to user cache directory.

func GetEnv

func GetEnv(key, defaultValue string) string

GetEnv returns an environment variable or a default value.

func GetEnvBool

func GetEnvBool(key string, defaultValue bool) bool

GetEnvBool returns an environment variable as bool or a default value.

func GetEnvDuration

func GetEnvDuration(key string, defaultValue time.Duration) time.Duration

GetEnvDuration returns an environment variable as duration or a default value.

func GetEnvInt

func GetEnvInt(key string, defaultValue int) int

GetEnvInt returns an environment variable as int or a default value.

func GetEnvInt64

func GetEnvInt64(key string, defaultValue int64) int64

GetEnvInt64 returns an environment variable as int64 or a default value.

func JSONResponse

func JSONResponse(w http.ResponseWriter, status int, data any)

JSONResponse writes a JSON response.

func ReadFile

func ReadFile(ctx context.Context, instance cc.Instance, path string) ([]byte, error)

ReadFile reads a file from an instance.

func WriteFiles

func WriteFiles(ctx context.Context, instance cc.Instance, files map[string][]byte) error

WriteFiles writes multiple files to an instance.

Types

type BaseConfig

type BaseConfig struct {
	Port            string
	LogLevel        string
	DefaultMemoryMB uint64
	DefaultTimeout  time.Duration
	MaxConcurrent   int
}

BaseConfig contains common configuration options.

func LoadBaseConfig

func LoadBaseConfig() BaseConfig

LoadBaseConfig loads base configuration from environment.

type Histogram

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

Histogram is a simple histogram implementation.

func NewHistogram

func NewHistogram() *Histogram

NewHistogram creates a new histogram.

func (*Histogram) Observe

func (h *Histogram) Observe(value float64)

Observe records a value.

func (*Histogram) Stats

func (h *Histogram) Stats() (float64, int64)

Stats returns sum and count.

type Metrics

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

Metrics collects Prometheus-compatible metrics.

func NewMetrics

func NewMetrics() *Metrics

NewMetrics creates a new metrics collector.

func (*Metrics) Counter

func (m *Metrics) Counter(name string, delta int64)

Counter increments a counter metric.

func (*Metrics) Gauge

func (m *Metrics) Gauge(name string, value int64)

Gauge sets a gauge metric.

func (*Metrics) Handler

func (m *Metrics) Handler() http.HandlerFunc

Handler returns an HTTP handler for /metrics endpoint.

func (*Metrics) Histogram

func (m *Metrics) Histogram(name string, value float64)

Histogram records a histogram observation.

func (*Metrics) Timer

func (m *Metrics) Timer(name string) func()

Timer returns a function that records duration when called.

type RunResult

type RunResult struct {
	Stdout   string        `json:"stdout"`
	Stderr   string        `json:"stderr"`
	ExitCode int           `json:"exit_code"`
	Duration time.Duration `json:"duration_ms"`
	TimedOut bool          `json:"timed_out,omitempty"`
	Error    string        `json:"error,omitempty"`
}

RunResult contains the result of running a command in a sandbox.

func RunCommand

func RunCommand(ctx context.Context, instance cc.Instance, name string, args ...string) RunResult

RunCommand runs a command in an instance and captures output.

func RunCommandWithEnv

func RunCommandWithEnv(ctx context.Context, instance cc.Instance, env []string, name string, args ...string) RunResult

RunCommandWithEnv runs a command with environment variables. Each env entry should be in "KEY=value" format.

func RunCommandWithStdin

func RunCommandWithStdin(ctx context.Context, instance cc.Instance, stdin io.Reader, name string, args ...string) RunResult

RunCommandWithStdin runs a command with stdin input.

type SandboxPool

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

SandboxPool manages a pool of pre-warmed sandbox instances.

func NewSandboxPool

func NewSandboxPool(source cc.InstanceSource, size int, opts ...cc.Option) *SandboxPool

NewSandboxPool creates a new sandbox pool.

func (*SandboxPool) Acquire

func (p *SandboxPool) Acquire(ctx context.Context) (cc.Instance, error)

Acquire gets an instance from the pool or creates a new one.

func (*SandboxPool) Close

func (p *SandboxPool) Close() error

Close closes all instances in the pool.

func (*SandboxPool) Release

func (p *SandboxPool) Release(instance cc.Instance)

Release returns an instance to the pool or closes it.

func (*SandboxPool) Warm

func (p *SandboxPool) Warm(ctx context.Context) error

Warm pre-warms the pool with instances.

type Server

type Server struct {
	*http.Server
	// contains filtered or unexported fields
}

Server wraps http.Server with graceful shutdown and common middleware.

func NewServer

func NewServer(addr string, handler http.Handler, logger *slog.Logger) *Server

NewServer creates a new server with the given handler.

func (*Server) HealthHandler

func (s *Server) HealthHandler() http.HandlerFunc

HealthHandler returns a health check handler.

func (*Server) IsHealthy

func (s *Server) IsHealthy() bool

IsHealthy returns whether the server is healthy.

func (*Server) IsReady

func (s *Server) IsReady() bool

IsReady returns whether the server is ready.

func (*Server) ListenAndServeWithGracefulShutdown

func (s *Server) ListenAndServeWithGracefulShutdown() error

ListenAndServeWithGracefulShutdown starts the server and handles graceful shutdown.

func (*Server) ReadyHandler

func (s *Server) ReadyHandler() http.HandlerFunc

ReadyHandler returns a readiness check handler.

func (*Server) SetReady

func (s *Server) SetReady(ready bool)

SetReady marks the server as ready to accept traffic.

Directories

Path Synopsis
Package testrunner provides a YAML-driven test framework for example services.
Package testrunner provides a YAML-driven test framework for example services.
cmd/runtest command
Command runtest executes YAML-driven tests for example services.
Command runtest executes YAML-driven tests for example services.

Jump to

Keyboard shortcuts

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