Documentation
¶
Overview ¶
Package preview implements server-local job preview leases and loopback proxying.
Index ¶
- Variables
- type Lease
- type Manager
- func (m *Manager) AcceptWorker(ctx context.Context, workerID string, conn *websocket.Conn)
- func (m *Manager) AttachWorkers(h WorkerHub)
- func (m *Manager) Close(id, reason string) error
- func (m *Manager) CloseAll()
- func (m *Manager) CloseJob(jobID string)
- func (m *Manager) Create(ctx context.Context, jobID, caller, host string, port, ttl int, ...) (Lease, string, error)
- func (m *Manager) CreateOnWorker(ctx context.Context, workerID, jobID, caller, host string, port, ttl int, ...) (Lease, string, error)
- func (m *Manager) Get(id string) (Lease, bool, error)
- func (m *Manager) List(jobID string) ([]Lease, error)
- func (m *Manager) NotePresence(workerID string, online bool)
- func (m *Manager) Renew(id string, ttl int, actor string) (Lease, error)
- func (m *Manager) Revoke(id, actor string) error
- func (m *Manager) String() string
- func (m *Manager) Sweep()
- type WorkerHub
Constants ¶
This section is empty.
Variables ¶
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.
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.
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 (*Manager) AcceptWorker ¶ added in v0.207.0
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
AttachWorkers wires the worker hub so leases can target jobs on workers. Without it a worker job is refused with ErrWorkerOffline.
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) NotePresence ¶ added in v0.207.0
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
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.
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.