preview

package
v0.211.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: MIT Imports: 21 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package preview implements server-local job preview leases and loopback proxying.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDisabled      = errors.New("job preview is disabled")
	ErrBadPort       = errors.New("port must be between 1 and 65535")
	ErrPoolExhausted = errors.New("preview port pool exhausted")
)

Refusals Create answers with, as values so the HTTP layer can attach a stable error code instead of matching message text.

View Source
var (
	// ErrWorkerOffline: the job's worker is not connected.
	ErrWorkerOffline = errors.New("preview worker is offline")
	// ErrWorkerOutdated: the worker's protocol predates wsproto.PreviewMinProtocolVersion.
	ErrWorkerOutdated = errors.New("preview worker needs an upgrade")
	// ErrWorkerLost: the worker process the lease was created against is gone.
	ErrWorkerLost = errors.New("preview worker instance changed")
	// ErrWorkerDial: the worker refused or failed to open the connection.
	ErrWorkerDial = errors.New("preview worker could not open the connection")
)

Refusals specific to a lease on a worker, as values so the HTTP layer can attach a stable error code instead of matching message text.

View Source
var ErrNotActive = errors.New("preview lease is not active")

ErrNotActive is returned when an operation needs a live lease and the lease is gone.

Functions

This section is empty.

Types

type Lease

type Lease struct {
	ID          string `json:"id"`
	JobID       string `json:"job_id"`
	CallerID    string `json:"caller_id"`
	WorkerID    string `json:"worker_id"`
	Port        int    `json:"port"`
	State       string `json:"state"`
	CreatedAt   int64  `json:"created_at"`
	ExpiresAt   int64  `json:"expires_at"`
	CloseReason string `json:"close_reason,omitempty"`
}

Lease is the non-secret view of a preview lease. Port is the preview port the browser opens (not the job's target port).

type Manager

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

func New

func New(store *jobstore.Store, cfg config.PreviewConfig) *Manager

func (*Manager) AcceptWorker added in v0.207.0

func (m *Manager) AcceptWorker(ctx context.Context, workerID string, conn *websocket.Conn)

AcceptWorker serves one preview-connect WebSocket the HTTP layer has already authenticated as workerID: it reads the hello, checks the one-time nonce against the rendezvous (worker, instance, lease, job, connection) and hands the connection to the waiting proxy dial, then blocks until that connection is finished.

func (*Manager) AttachWorkers added in v0.207.0

func (m *Manager) AttachWorkers(h WorkerHub)

AttachWorkers wires the worker hub so leases can target jobs on workers. Without it a worker job is refused with ErrWorkerOffline.

func (*Manager) Close

func (m *Manager) Close(id, reason string) error

func (*Manager) CloseAll

func (m *Manager) CloseAll()

func (*Manager) CloseJob

func (m *Manager) CloseJob(jobID string)

func (*Manager) Create

func (m *Manager) Create(ctx context.Context, jobID, caller, host string, port, ttl int, jobRunning bool) (Lease, string, error)

Create makes a lease for a job that runs on this server.

func (*Manager) CreateOnWorker added in v0.207.0

func (m *Manager) CreateOnWorker(ctx context.Context, workerID, jobID, caller, host string, port, ttl int, jobRunning bool) (Lease, string, error)

CreateOnWorker makes a lease for a job that runs on workerID: the proxy reaches the job's port through that worker. It refuses a worker that is offline (ErrWorkerOffline) or too old to serve previews (ErrWorkerOutdated) before anything is allocated.

func (*Manager) Get

func (m *Manager) Get(id string) (Lease, bool, error)

func (*Manager) List

func (m *Manager) List(jobID string) ([]Lease, error)

func (*Manager) NotePresence added in v0.207.0

func (m *Manager) NotePresence(workerID string, online bool)

NotePresence reacts to a worker coming online or going offline: when it went offline, or came back as a different process, every lease bound to the old process is closed with reason worker_lost and never rebound to the new one.

func (*Manager) Renew added in v0.205.0

func (m *Manager) Renew(id string, ttl int, actor string) (Lease, error)

Renew moves an active lease's expiry to now + ttl (ttl defaults and is capped like on create, so a lease never runs longer than max_ttl_sec from the renewal) and restarts its timer.

func (*Manager) Revoke added in v0.205.0

func (m *Manager) Revoke(id, actor string) error

Revoke closes a lease on behalf of a caller; the audit row names who did it.

func (*Manager) String

func (m *Manager) String() string

func (*Manager) Sweep

func (m *Manager) Sweep()

type WorkerHub added in v0.207.0

type WorkerHub interface {
	LiveInstance(workerID string) (string, bool)
	WorkerProtocol(workerID string) (int, bool)
	OpenPreview(workerID string, o wsproto.PreviewOpen) error
}

WorkerHub is what the manager needs from the worker hub: liveness, the negotiated protocol and the one control frame. *wshub.Hub satisfies it.

Jump to

Keyboard shortcuts

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