function

package
v0.0.0-...-4009ad3 Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2026 License: MIT Imports: 24 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithCallerContext

func WithCallerContext(ctx context.Context, cc CallerContext) context.Context

WithCallerContext injects a CallerContext into the execution context.

func WithFlagContext

func WithFlagContext(ctx context.Context, flagCtx string) context.Context

WithFlagContext marks a function invocation as running under a specific flag variant (format: "flagKey:variantKey"). table_put uses this to tag new records.

Types

type CallerContext

type CallerContext struct {
	UserID   string   `json:"user_id"`
	Role     string   `json:"role"`
	TenantID string   `json:"tenant_id"`
	Email    string   `json:"email"`
	Groups   []string `json:"groups"`
}

CallerContext holds the identity of the user who triggered a function invocation.

type Engine

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

Engine wraps a Wazero runtime for executing sandboxed WASM functions. Each Invoke call instantiates a fresh module with a hard execution deadline, preventing runaway functions from starving shared CPU resources.

func NewEngine

func NewEngine(ctx context.Context) *Engine

NewEngine creates an Engine. The caller must call Close when done. WithCloseOnContextDone enables hard interruption of tight WASM loops when the execution context deadline is exceeded.

func (*Engine) Close

func (e *Engine) Close(ctx context.Context)

Close releases all resources held by the runtime.

func (*Engine) Invoke

func (e *Engine) Invoke(ctx context.Context, wasmPath string, timeout time.Duration) ([]byte, error)

Invoke loads, instantiates, and calls the exported "handle" function in a WASM module at wasmPath. The execution is bounded by timeout to guard against infinite loops in user-supplied functions.

func (*Engine) InvokeWASI

func (e *Engine) InvokeWASI(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, m *WASIMetrics) ([]byte, error)

InvokeWASI loads and runs a WASI preview1 WASM module. The module's main() is called automatically on instantiation; stdout is captured and returned. m is populated with compute metrics if non-nil.

func (*Engine) InvokeWASIStream

func (e *Engine) InvokeWASIStream(ctx context.Context, wasmBytes []byte, timeout time.Duration, deps *HostDeps, w io.Writer, m *WASIMetrics) error

InvokeWASIStream runs a WASI WASM module and writes stdout directly to w as bytes arrive, enabling real-time streaming to callers. m is populated with compute metrics if non-nil.

type Function

type Function struct {
	ID          string    `json:"id"`
	Name        string    `json:"name"`
	Description string    `json:"description"`
	Language    string    `json:"language"`
	Source      string    `json:"source"`
	Runtime     string    `json:"runtime"`
	Status      string    `json:"status"`
	Error       string    `json:"error,omitempty"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
}

Function is a stored, potentially compiled, user function.

type FunctionInvocation

type FunctionInvocation struct {
	ID              string     `json:"id"`
	FunctionID      string     `json:"function_id"`
	StartedAt       time.Time  `json:"started_at"`
	CompletedAt     *time.Time `json:"completed_at,omitempty"`
	Success         bool       `json:"success"`
	Output          string     `json:"output,omitempty"`
	Error           string     `json:"error,omitempty"`
	ExecutionMs     *int64     `json:"execution_ms,omitempty"`
	PeakMemoryBytes int64      `json:"peak_memory_bytes"`
	HostCalls       int        `json:"host_calls"`
	OutputSizeBytes int        `json:"output_size_bytes"`
	TraceID         string     `json:"trace_id,omitempty"`
}

FunctionInvocation records a single execution of a function.

type FunctionVersion

type FunctionVersion struct {
	ID         string    `json:"id"`
	FunctionID string    `json:"function_id"`
	Version    int       `json:"version"`
	Source     string    `json:"source,omitempty"`
	CreatedAt  time.Time `json:"created_at"`
}

FunctionVersion records a single deployment of a function.

type HostDeps

type HostDeps struct {
	Storage storage.BucketAdapter
	Flags   *feature.Engine
	Store   *Store        // for fn_invoke; may be nil to break init cycle
	Tables  *table.Engine // may be nil when tables feature is not wired
	Tracer  *tracing.Recorder
	DB      *sql.DB // for custom metrics; may be nil
	FnID    string  // current function ID (for metric tagging)
	InvID   string  // current invocation ID (for metric tagging)
	TraceID string  // current trace ID propagated from context
}

HostDeps are the flagbase services exposed to WASM functions via the "flagbase" host module.

type Store

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

Store manages function lifecycle: persistence, compilation, and invocation.

func NewStore

func NewStore(db *sql.DB, store *storage.LocalAdapter, engine *Engine, flags *feature.Engine) *Store

func (*Store) Create

func (s *Store) Create(ctx context.Context, name, description, language, source string) (*Function, error)

Create persists a new JavaScript function record and validates it synchronously. For Go/WASM functions, use Upload instead.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

Delete removes a function and its stored artifact.

func (*Store) DeployVersion

func (s *Store) DeployVersion(ctx context.Context, id string, wasmBytes []byte) (*FunctionVersion, error)

DeployVersion uploads a new WASM artifact for an existing function, overwrites the active artifact, and records a new version entry.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Function, error)

Get returns a single function by ID.

func (*Store) GetVersion

func (s *Store) GetVersion(ctx context.Context, functionID, versionID string) (*FunctionVersion, error)

GetVersion returns a single version record by ID, including source snapshot.

func (*Store) Invoke

func (s *Store) Invoke(ctx context.Context, id string, timeout time.Duration) ([]byte, error)

Invoke executes a ready function and returns its output.

func (*Store) InvokeStream

func (s *Store) InvokeStream(ctx context.Context, id string, timeout time.Duration, w io.Writer) error

InvokeStream executes a ready function, writing its stdout output to w as bytes arrive. This enables real-time streaming to HTTP clients.

func (*Store) InvokeStreamWithEvent

func (s *Store) InvokeStreamWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte, w io.Writer) error

InvokeStreamWithEvent executes a function with an event payload, streaming stdout to w in real-time.

func (*Store) InvokeWithEvent

func (s *Store) InvokeWithEvent(ctx context.Context, id string, timeout time.Duration, eventData []byte) ([]byte, error)

InvokeWithEvent executes a function with an event payload readable via the event_read host call.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]Function, error)

List returns all functions ordered by creation date descending.

func (*Store) ListInvocations

func (s *Store) ListInvocations(ctx context.Context, fnID string) ([]FunctionInvocation, error)

ListInvocations returns the 100 most recent invocations for a function.

func (*Store) ListVersions

func (s *Store) ListVersions(ctx context.Context, functionID string) ([]FunctionVersion, error)

ListVersions returns all deployment versions for a function, oldest first.

func (*Store) Update

func (s *Store) Update(ctx context.Context, id, name, description, source string) (*Function, error)

Update recompiles a JavaScript function from new source and stores a new version.

func (*Store) Upload

func (s *Store) Upload(ctx context.Context, name, description string, wasmBytes []byte) (*Function, error)

Upload persists a pre-compiled WASM artifact. The caller is responsible for compiling Go source to WASI preview1 WASM (e.g. via `flagbase fn build`).

func (*Store) WithTables

func (s *Store) WithTables(t *table.Engine)

WithTables injects the table engine so WASM functions can call table host functions.

func (*Store) WithTracer

func (s *Store) WithTracer(t *tracing.Recorder)

WithTracer injects the distributed tracing recorder.

type WASIMetrics

type WASIMetrics struct {
	PeakMemoryBytes uint32
	HostCalls       int
}

WASIMetrics holds performance counters captured during a single WASM invocation.

Jump to

Keyboard shortcuts

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