Documentation
¶
Overview ¶
Package wire is the internal bridge-to-server format described in docs/wire.md: ACP messages in numbered envelopes over a WebSocket, with cumulative acks, heartbeats, a send window and replay after a reconnect.
Index ¶
- Constants
- Variables
- type Endpoint
- func (e *Endpoint) Attached() bool
- func (e *Endpoint) Close()
- func (e *Endpoint) Detach(code websocket.StatusCode, reason string)
- func (e *Endpoint) LastReceived() int64
- func (e *Endpoint) OutboxBytes() int64
- func (e *Endpoint) Send(raw json.RawMessage)
- func (e *Endpoint) SendControl(f Frame)
- func (e *Endpoint) Serve(ctx context.Context, conn *websocket.Conn, peerLastSeq int64) error
- func (e *Endpoint) SetDiscard(on bool)
- func (e *Endpoint) SpoolError() error
- type Frame
- type Options
- type RemoteError
Constants ¶
const ( HeaderLinkID = "Actp-Link-Id" HeaderLastSeq = "Actp-Last-Seq" HeaderClientName = "Actp-Client-Name" HeaderPromptTimeout = "Actp-Prompt-Timeout" )
Headers on the link upgrade request and response.
const ( CloseReplaced websocket.StatusCode = 4001 CloseLinkEnded websocket.StatusCode = 4002 CloseProtocolError websocket.StatusCode = 4003 CloseDead websocket.StatusCode = 4004 )
WebSocket close codes.
const ( TMsg = "msg" TAck = "ack" TPing = "ping" TPong = "pong" TBye = "bye" TError = "error" )
Frame types.
const ( ErrAgentExited = "agent_exited" ErrSpoolFull = "spool_full" ErrServerShutdown = "server_shutdown" ErrEndedByAdmin = "ended_by_admin" ErrLinkEnded = "link_ended" ErrSequenceGap = "sequence_gap" ErrProtocol = "protocol_error" )
Error codes sent in error frames.
Variables ¶
var ( // ErrBye means the peer said its client has left. ErrBye = errors.New("peer said bye") // ErrDead means nothing arrived from the peer for too long. ErrDead = errors.New("peer went silent") // ErrReplaced means a newer socket took over the link. ErrReplaced = errors.New("replaced by a newer connection") // ErrDetached means the socket was closed on purpose by this side. ErrDetached = errors.New("detached") )
var Versions = []string{"actp.v1"}
Wire versions, newest first. A server and bridge pick the newest they share.
Functions ¶
This section is empty.
Types ¶
type Endpoint ¶
type Endpoint struct {
// contains filtered or unexported fields
}
Endpoint is one side of a link. It outlives sockets: messages sent while no socket is attached wait in the outbox and go out after the next Serve.
func NewEndpoint ¶
func NewEndpoint(opts Options, deliver func(json.RawMessage) error) *Endpoint
NewEndpoint creates an endpoint. deliver is called once per peer message, in order; the message counts as processed when it returns nil.
func (*Endpoint) Close ¶
func (e *Endpoint) Close()
Close deletes the spool file. The endpoint must not be used afterwards.
func (*Endpoint) Detach ¶
func (e *Endpoint) Detach(code websocket.StatusCode, reason string)
Detach closes the current socket, if any, with the given code.
func (*Endpoint) LastReceived ¶
LastReceived is the highest peer sequence number processed.
func (*Endpoint) OutboxBytes ¶
OutboxBytes is the size of messages waiting for an ack.
func (*Endpoint) Send ¶
func (e *Endpoint) Send(raw json.RawMessage)
Send queues an ACP message for the peer.
func (*Endpoint) SendControl ¶
SendControl queues a control frame (bye, error) on the current socket.
func (*Endpoint) Serve ¶
Serve runs the link over conn until the socket ends. peerLastSeq is the highest of our messages the peer has processed; everything after it is resent.
func (*Endpoint) SetDiscard ¶
SetDiscard drops queued and future outgoing messages. Used once nobody will read them.
func (*Endpoint) SpoolError ¶
SpoolError reports a failure to use the spool file, if any.
type Frame ¶
type Frame struct {
T string `json:"t"`
Seq int64 `json:"seq,omitempty"`
Ack int64 `json:"ack,omitempty"`
Msg json.RawMessage `json:"msg,omitempty"`
Code string `json:"code,omitempty"`
Message string `json:"message,omitempty"`
Data json.RawMessage `json:"data,omitempty"`
}
Frame is one WebSocket text frame.
type Options ¶
type Options struct {
HeartbeatEvery time.Duration // send something at least this often (10 s)
DeadAfter time.Duration // peer silent this long → socket is dead (25 s)
AckDelay time.Duration // ack received messages within this delay (1 s)
AckEvery int64 // or after this many messages (50)
WindowMsgs int64 // stop sending with this many unacked (1000)
WindowBytes int64 // or this many bytes unacked (4 MB)
HeartbeatPing bool // heartbeat with ping instead of ack
Tick time.Duration // writer housekeeping interval (100 ms)
// SpoolPath, when set, is a file that unacked messages spill into once
// MemoryLimit bytes are held in memory (8 MB). The file is private to the user.
SpoolPath string
MemoryLimit int64
}
Options tune liveness and flow control. Zero values use the defaults from docs/wire.md.
type RemoteError ¶
type RemoteError struct {
Code string
Message string
Data json.RawMessage
}
RemoteError is an error frame received from the other side.
func (*RemoteError) Error ¶
func (e *RemoteError) Error() string
func (*RemoteError) Fatal ¶
func (e *RemoteError) Fatal() bool
Fatal reports whether the link has ended, as opposed to only this socket.