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
- func Path(dir string) string
- func RecordPath(dir, p string) error
- func WriteFrame(w io.Writer, h Header, body []byte) error
- type Client
- func (c *Client) Close() error
- func (c *Client) Decide(ctx context.Context, req *responses.Request, meta responses.Meta) (responses.Rewrite, bool)
- func (c *Client) Observe(meta responses.Meta, served string, usage responses.Usage, status int)
- func (c *Client) PassThrough(meta responses.Meta)
- func (c *Client) Up(ctx context.Context) bool
- type ClientOptions
- type Handler
- type Header
- type Server
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" TypePassThrough = "passthrough" TypeError = "error" )
Frame types. The edge sends hello, decide, passthrough 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 ¶
func Path ¶ added in v0.3.1
Path is the link socket of the brain whose home state dir is dir: SocketName in dir, or the path the brain recorded there because that one was too long for a unix socket. The edge reads it before each dial.
func RecordPath ¶ added in v0.3.1
RecordPath writes pathFile when the link socket p is outside dir, and removes a stale one when it is not.
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. Passthrough signals share the ordered writer queue, so a request's signal precedes its response observation and a later decide waits for all earlier queued events.
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 event writer. Queued events 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.
func (*Client) Observe ¶
Observe queues the observation for the brain, or drops it when the queue is full.
func (*Client) PassThrough ¶ added in v0.7.2
PassThrough queues a metadata-only signal to clear the brain's awaiting routed reply. It shares the writer queue with observations and is not dropped when the queue is full, preserving frame order before the response.
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.