Documentation
¶
Overview ¶
Package link is the edge-brain socket of stage 2 (plan decision M7). The edge holds the provider port and asks the brain, over a 0600 unix socket, for each request's rewrite and reports each response's usage. A frame is a 4-byte length, a JSON header line and the raw request body; bodies never leave the machine. The client fails open: a brain that is down, slow or gone mid-call means "forward unchanged".
Index ¶
Constants ¶
const ( DialTimeout = 200 * time.Millisecond BackoffMin = 50 * time.Millisecond BackoffMax = 2 * time.Second FailuresToOpen = 3 BreakerCooldown = 10 * time.Second ObserveQueue = 256 )
Redial, breaker and queue rules. A dial includes the hello and is bounded by DialTimeout, because a decide waits for it.
const ( TypeHello = "hello" TypeDecide = "decide" TypeRewrite = "rewrite" TypeNone = "none" TypeObserve = "observe" TypeError = "error" )
Frame types. The edge sends hello, decide and observe; the brain answers hello, rewrite or none per decide, and error before it closes a connection it refuses.
const MaxFrameBytes = MaxHeaderBytes + 1 + proxy.MaxBodyBytes
MaxFrameBytes caps a frame after its length prefix: a header, its newline and a body as large as the proxy accepts.
const MaxHeaderBytes = 64 << 10
MaxHeaderBytes caps a frame's JSON header.
const SocketMode os.FileMode = 0o600
SocketMode is the permission the socket is created with.
const SocketName = "brain.sock"
SocketName is the brain's link socket inside the home's state dir.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the edge's end of the link. Every failure means "no rewrite": the brain down (redial after a backoff), slow (the call's deadline, and a breaker that skips the brain for BreakerCooldown after FailuresToOpen timeouts in a row) or gone mid-call. Observe never blocks: it queues and drops when ObserveQueue is full. A decide is written after the observations queued before it, so the brain applies a reply's usage before the next step's decide.
func NewClient ¶
func NewClient(o ClientOptions) *Client
NewClient returns a Client and starts its observe writer. It dials on the first call, not here, so an edge starts while its brain is down.
func (*Client) Close ¶
Close drops the connection and stops the observe writer. Queued observations are lost.
func (*Client) Decide ¶
func (c *Client) Decide(ctx context.Context, req *responses.Request, meta responses.Meta) (responses.Rewrite, bool)
Decide asks the brain for req's rewrite under the call's deadline.
type ClientOptions ¶
type ClientOptions struct {
// Path returns the brain's socket path; each dial calls it, so an edge
// follows a brain that restarts on another path.
Path func() string
HomeID string
Timeout time.Duration
Logger *slog.Logger
}
ClientOptions configure a Client. Timeout bounds each Decide on top of the caller's context; zero means proxy.DefaultDecideTimeout.
type Header ¶
type Header struct {
Type string `json:"type"`
ID uint64 `json:"id,omitempty"`
HomeID string `json:"home_id,omitempty"`
Meta *responses.Meta `json:"meta,omitempty"`
DeadlineUnixMs int64 `json:"deadline_unix_ms,omitempty"`
Rewrite *responses.Rewrite `json:"rewrite,omitempty"`
Served string `json:"served,omitempty"`
Usage *responses.Usage `json:"usage,omitempty"`
Status int `json:"status,omitempty"`
Error string `json:"error,omitempty"`
}
Header is a frame's JSON header. ID pairs a decide with its rewrite or none. Meta holds parsed turn metadata only, never raw request headers. DeadlineUnixMs is when the edge stops waiting for a decide's answer, in Unix milliseconds. It is absolute because edge and brain share the host's clock: the brain's deadline then covers the frame's transit and parse too, and its classifier call cannot outlive the edge's wait.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the brain's end of the link. It accepts edges of its own home only, runs each decide in its own goroutine (a slow classifier call holds up only its own request) and applies observes in arrival order.