link

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
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.

View Source
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.

View Source
const MaxHeaderBytes = 64 << 10

MaxHeaderBytes caps a frame's JSON header.

View Source
const SocketMode os.FileMode = 0o600

SocketMode is the permission the socket is created with.

View Source
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

func Path(dir string) string

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

func RecordPath(dir, p string) error

RecordPath writes pathFile when the link socket p is outside dir, and removes a stale one when it is not.

func WriteFrame

func WriteFrame(w io.Writer, h Header, body []byte) error

WriteFrame writes one frame: a big-endian uint32 length, the header as one JSON line, then body. The body is written as is, without a copy.

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

func (c *Client) Close() error

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.

func (*Client) Observe

func (c *Client) Observe(meta responses.Meta, served string, usage responses.Usage, status int)

Observe queues the observation for the brain, or drops it when the queue is full.

func (*Client) Up

func (c *Client) Up(ctx context.Context) bool

Up reports whether the brain is reachable: a live connection, or a dial now. While a failed dial's backoff runs it is false without a dial.

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 Handler

type Handler interface {
	proxy.Decider
	proxy.Observer
}

Handler decides and observes for the brain; *route.Engine is one.

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.

func ReadFrame

func ReadFrame(r io.Reader) (Header, []byte, error)

ReadFrame reads one frame written by WriteFrame. An empty body is nil.

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.

func Listen

func Listen(path, homeID string, h Handler, log *slog.Logger) (*Server, error)

Listen binds the socket at path with mode 0600 and starts serving. The parent dir must be private (statedir.CheckPrivate): the mode is set right after bind, and the dir closes that window. A stale socket file is replaced; a live one or a non-socket file is refused.

func (*Server) Close

func (s *Server) Close() error

Close stops accepting, cancels in-flight decides, drops every edge and removes the socket file.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL