watchv1
import watchv1 "pocketone.dev/ext/contracts/instance/watch/v1"
Package watchv1 is the instance.watch/v1 contract: an edge follows a control plane (a mothership) lazily. It opens one stream, asks for a hostname the first time a request names it, and from then on hears about every change to what that hostname serves, until it says it no longer cares. Nothing is sent that no edge asked for, except the list of edges and the instances an edge must hold whatever its traffic (its standing set).
A wire contract at /api/_c/instance.watch/v1. The mothership provides it (ext/mothership); edges call it (ext/mothership/client).
The order of things is the promise:
- The stream's first event is a hello: the connection id, this edge's record, every edge and the standing set.
- Watch registers the hostname on the connection before it reads anything, then sends the answer as an event on the stream (never in the reply), so the answer and every later change arrive in the order they happened.
- A Watch naming no live connection (the edge's stream is down) gets the answer in its reply, with nothing registered: the caller may use it once, but nobody will tell it when it changes.
- A change is sent only when the answer differs from the one sent last for that hostname on that connection.
- A dropped stream forgets every watch. On reconnect the edge starts over: what it cached is no longer watched.
Source: ext/contracts/instance/watch/v1.
Constants
const (
KindHello = "hello" // the first event: Conn, Self, Edges, Standing
KindHost = "host" // Host's answer (the first after a Watch, or a change)
KindEdges = "edges" // the edges list changed: Edges
KindStanding = "standing" // this edge's standing set changed: Standing (all of it)
KindPing = "ping" // nothing happened: sent when the stream was idle for PingEvery, so a dead one shows
)
Event kinds, in Event.Kind.
const PingEvery = 25 * time.Second
PingEvery is how often an idle stream gets a ping; an edge that hears nothing for a few of them reconnects.
Variables
var Contract = extkit.NewContract[Watcher]("instance.watch/v1", extkit.One)
Contract is instance.watch/v1.
var ErrUnknownConn = &extkit.Error{Code: "unknown_conn", Message: "no open stream has that connection id", Status: 404}
ErrUnknownConn: the connection named isn't open (on Unwatch; Watch answers in its reply instead).
var W = extkit.NewWire(Contract, "fleet", func(inner Watcher, g *extkit.Guard[Watcher]) Watcher {
return &guardWatcher{inner, g}
},
extkit.Op[Watcher]{
Name: "stream",
Desc: "Opens this edge's stream: a hello first, then answers and changes for the hostnames it watches, the edges list when it changes, and its standing set when that changes.",
Method: "GET",
Path: "stream",
In: reflect.TypeOf(StreamInput{}),
Out: extkit.OutStream,
Invoke: func(ctx context.Context, impl Watcher, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in StreamInput
if err := extkit.DecodeQuery(req.Query, &in); err != nil {
return nil, err
}
return impl.Stream(ctx, a, in)
},
},
extkit.Op[Watcher]{
Name: "watch",
Desc: "Registers a hostname on a stream and sends its answer there (or, with no live stream, in the reply).",
Method: "POST",
Path: "watch",
In: reflect.TypeOf(WatchInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Watcher, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in WatchInput
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Watch(ctx, a, in)
},
},
extkit.Op[Watcher]{
Name: "unwatch",
Desc: "Stops changes for a hostname on a stream.",
Method: "POST",
Path: "unwatch",
In: reflect.TypeOf(UnwatchInput{}),
Out: extkit.OutJSON,
Invoke: func(ctx context.Context, impl Watcher, a extkit.Auth, req extkit.OpRequest) (any, error) {
var in UnwatchInput
if err := extkit.DecodeInput(req.JSON, &in); err != nil {
return nil, err
}
return impl.Unwatch(ctx, a, in)
},
},
)
W is instance.watch/v1's wire side: routes at /api/_c/instance.watch/v1/<op>, action hooks, MCP tools and the client.
Functions
OnStream
func OnStream(owner any, fn func(e *extkit.CallEvent, in *StreamInput) error)
OnStream binds fn to stream calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnUnwatch
func OnUnwatch(owner any, fn func(e *extkit.CallEvent, in *UnwatchInput) error)
OnUnwatch binds fn to unwatch calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
OnWatch
func OnWatch(owner any, fn func(e *extkit.CallEvent, in *WatchInput) error)
OnWatch binds fn to watch calls on owner (an app, or a host's scope for calls the host answers). Call e.Next() to continue; return an error to refuse. in is the call's input; changes reach the provider.
Types
Answer
type Answer struct {
Found bool `json:"found"`
Instance *sourcev1.Instance `json:"instance,omitempty"`
}
Answer is what a hostname serves: an instance, or nothing (Found false; "no such host" is an answer too, watched like any other, so the instance that later takes the name reaches the edge).
Client
type Client struct {
// contains filtered or unexported fields
}
Client is instance.watch/v1 over HTTP, for the CLI and other apps. It implements Watcher; the token it was made with is the credential, so the extkit.Auth argument is ignored.
NewClient
func NewClient(baseURL, token string, hc *http.Client) *Client
NewClient calls instance.watch/v1 on the app at baseURL with token (a PocketBase auth token or an API token).
Client.Stream
func (c *Client) Stream(ctx context.Context, _ extkit.Auth, in StreamInput) (extkit.Stream, error)
Client.Unwatch
func (c *Client) Unwatch(ctx context.Context, _ extkit.Auth, in UnwatchInput) (UnwatchResult, error)
Client.Watch
func (c *Client) Watch(ctx context.Context, _ extkit.Auth, in WatchInput) (WatchResult, error)
Edge
type Edge struct {
ID string `json:"id"` // the control plane's record id
Name string `json:"name"` // the edge id, such as lax-1
InternalURL string `json:"internal_url,omitempty"`
Region string `json:"region,omitempty"`
Status string `json:"status,omitempty"`
LastSeen time.Time `json:"last_seen,omitzero"`
}
Edge is one edge of the fleet, as the stream sends it. A Remote of "edge:<ID>" or "edge:@<Name>" names it.
Event
type Event struct {
Kind string `json:"kind"`
// Seq numbers a connection's events from 1, so an edge can tell it missed nothing.
Seq uint64 `json:"seq"`
Conn string `json:"conn,omitempty"` // hello
Self *Edge `json:"self,omitempty"` // hello: this edge's record, if it has one
Edges []Edge `json:"edges,omitempty"` // hello, edges
Standing []sourcev1.Instance `json:"standing,omitempty"` // hello, standing
Host string `json:"host,omitempty"` // host
Answer *Answer `json:"answer,omitempty"` // host
}
Event is one entry on the stream (the SSE event is "stream", the data this JSON).
StreamInput
type StreamInput struct {
Edge string `json:"edge"`
}
StreamInput names the edge opening the stream (its edge id, such as lax-1). Edges sign in as superusers today, so the edge says who it is; a per-edge credential will carry it instead.
UnwatchInput
type UnwatchInput struct {
Conn string `json:"conn"`
Host string `json:"host"`
}
UnwatchInput is a hostname to stop watching on a connection.
UnwatchResult
type UnwatchResult struct {
Removed bool `json:"removed"`
}
UnwatchResult says whether the hostname was watched on the connection.
WatchInput
type WatchInput struct {
Conn string `json:"conn"`
Edge string `json:"edge"` // the edge asking, for an answer in the reply (no live Conn)
Host string `json:"host"`
}
WatchInput is a hostname to watch on a connection (Conn from the stream's hello). An empty or unknown Conn gets the answer in the reply, unwatched.
WatchResult
type WatchResult struct {
Registered bool `json:"registered"`
Answer *Answer `json:"answer,omitempty"`
}
WatchResult says whether the hostname is watched. Registered: its answer comes on the stream. Not registered: Answer is the answer, for this once.
Watcher
type Watcher interface {
// Stream opens this edge's stream: a hello first, then answers and changes for the hostnames it watches, the
// edges list when it changes, and its standing set when that changes.
//
//op: GET stream
Stream(ctx context.Context, a extkit.Auth, in StreamInput) (extkit.Stream, error)
// Watch registers a hostname on a stream and sends its answer there (or, with no live stream, in the reply).
//
//op: POST watch
Watch(ctx context.Context, a extkit.Auth, in WatchInput) (WatchResult, error)
// Unwatch stops changes for a hostname on a stream.
//
//op: POST unwatch
Unwatch(ctx context.Context, a extkit.Auth, in UnwatchInput) (UnwatchResult, error)
}
Watcher lets edges follow the instances behind the hostnames they serve.
Questions and comments
Comments are a GitHub Discussions thread; sign in with GitHub to post.