gate

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 26 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 gate is the runtime gate: the process on the runtime's port. It answers what the Go runtime already does — slice by slice, each switched per machine in state-mode.json — and hands everything else, sockets included, to the old runtime on an inner port. Callers (the cluster through the machine agent's tunnels, the agent's relays, the CLIs) see one port.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConnContext

func ConnContext(ctx context.Context, c net.Conn) context.Context

ConnContext is for http.Server.ConnContext: it lets the gate find the connection under an upgraded request.

func Encode

func Encode(v any) []byte

Encode is how an answer is written: like the previous runtime's JSON.stringify, no escaping of <, > and &, no trailing newline. A route that hashes its body (an ETag) hashes these bytes.

Types

type BodyRoute

type BodyRoute struct {
	Slice string
	Match func(*http.Request) bool
	// Serve writes the whole answer, a refusal included. It must only read: in the shadow it runs a second time.
	Serve http.HandlerFunc
}

BodyRoute is a read-only route whose answer is not only JSON — a file, a picture, a range of a video. Like a JSONRoute it is answered while its slice is on and shadowed while "<slice>-shadow" is: the old runtime answers, the Go handler writes into a recorder, and status, the headers a caller acts on and the body bytes are compared.

type Config

type Config struct {
	// Listen is the runtime's port, the one every caller knows.
	Listen string
	// Upstream is the old runtime on its inner port; empty runs the gate standalone (see Standalone).
	Upstream string
	// DataDir holds state-mode.json, where the machine agent writes which slices are on.
	DataDir string
	Version string
	// CountEvery is how often the paths still handed to the old runtime are logged.
	CountEvery time.Duration
	// RecordFile, when set, gets every request through the gate as a JSON line (credentials left out), for replay.
	RecordFile string
	// TokenHeaders name the headers that carry a token; their values are never recorded.
	TokenHeaders []string
}

Config is what the gate takes from its environment.

func LoadConfig

func LoadConfig() (Config, error)

LoadConfig reads the environment.

func (Config) Standalone

func (c Config) Standalone() bool

Standalone: no old runtime behind the gate. Every slice is on, whatever state-mode.json says (there is nobody else to answer), no shadow runs (there is nothing to compare with), and what no route takes gets the fallback's answer.

type Gate

type Gate struct {
	// contains filtered or unexported fields
}

Gate is the handler on the runtime's port.

func New

func New(cfg Config, routes ...Route) (*Gate, error)

func (*Gate) Add

func (f *Gate) Add(routes ...Route)

Add appends routes; call before serving.

func (*Gate) AsBodyRoutes

func (f *Gate) AsBodyRoutes(routes ...BodyRoute) []Route

AsBodyRoutes turns body routes into gate routes: answered while the slice is on, shadowed while "<slice>-shadow" is.

func (*Gate) AsRoutes

func (f *Gate) AsRoutes(routes ...JSONRoute) []Route

AsRoutes turns JSON routes into gate routes: answered while the slice is on, shadowed while "<slice>-shadow" is.

func (*Gate) CloseProxied

func (f *Gate) CloseProxied(slice string) int

CloseProxied closes the proxied sockets of the paths a slice takes over, for a switch the gate does not see itself (a slice that also needs another one running).

func (*Gate) HandOverSockets

func (f *Gate) HandOverSockets(slice string, paths ...string)

HandOverSockets names upgraded paths the slice takes over: proxied ones are closed when it switches on.

func (*Gate) LogCounts

func (f *Gate) LogCounts()

LogCounts writes, and resets, which paths went to the old runtime, how often and how long they took: the day it stays empty the old runtime can go, and until then it shows what the hop costs.

func (*Gate) OnSliceChange

func (f *Gate) OnSliceChange(slice string, fn func(on bool))

OnSliceChange runs fn whenever the slice switches (after the gate closed the proxied sockets it takes over); the Go side closes its own sockets there when the slice goes off.

func (*Gate) Proxy

func (f *Gate) Proxy(w http.ResponseWriter, r *http.Request)

Proxy hands a request to the old runtime as the gate does for paths it does not answer (to the fallback on a standalone gate).

func (*Gate) RefreshSessions

func (f *Gate) RefreshSessions(s SessionRefresh)

RefreshSessions turns the refresh on for every API request a Go route answers.

func (*Gate) ServeHTTP

func (f *Gate) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Gate) SetFallback

func (f *Gate) SetFallback(h http.Handler)

SetFallback names what answers, on a standalone gate, a request no route took (by default the old runtime's "Not found"); call before serving.

func (*Gate) SliceOn

func (f *Gate) SliceOn(slice string) bool

SliceOn is whether a slice is switched on now.

func (*Gate) Standalone

func (f *Gate) Standalone() bool

Standalone says whether the gate runs without an old runtime behind it.

func (*Gate) WatchSlices

func (f *Gate) WatchSlices(ctx context.Context)

WatchSlices notices switches of the slices with handed-over sockets or hooks, once a second, until ctx ends.

type JSONRoute

type JSONRoute struct {
	Slice string
	Match func(*http.Request) bool
	// Compute is the whole answer, a refusal to an unauthorized caller included: status, value (encoded as JSON; nil
	// for an empty body) and extra headers.
	Compute func(*http.Request) (int, any, http.Header)
	// Volatile are fields of the answer that differ between any two reads (a clock): the shadow compares only that both
	// answers have them. A name is a top-level field, or a dotted path into the answer where "*" stands for every key
	// of an object or every element of an array ("machines.*.uptimeSec").
	Volatile []string
	// Shape: the answer carries data of an external service that may change between the two reads (a pipeline that
	// finished, an issue moved on); the shadow compares the status and the answer's structure, not its values.
	Shape bool
}

JSONRoute is a read-only route a slice answers with JSON. Before the slice is switched on it can run in the shadow: the old runtime answers the page, the Go answer is computed next to it and the two are compared, so a slice is switched on only after its answers have matched on live traffic.

type OldRuntime

type OldRuntime struct {
	Upstream string
	DataDir  string
	// TokenHeaders carry the cli-token; the old runtime accepts the first one it knows.
	TokenHeaders []string
	Client       *http.Client
}

OldRuntime calls the previous runtime's internal routes for what the Go slices must tell it while it still keeps the state they touch. Calls carry the runtime's cli-token.

func (*OldRuntime) CLITokenOK

func (o *OldRuntime) CLITokenOK(r *http.Request) bool

CLITokenOK checks a request to the gate's internal routes: the runtime's cli-token in one of the headers.

func (*OldRuntime) Post

func (o *OldRuntime) Post(path string, body, out any) bool

Post sends body to path and decodes a JSON answer into out (if given). False when the call did not succeed, and at once without an old runtime (a standalone gate).

type Raw

type Raw struct {
	ContentType string
	Body        []byte
}

Raw is an answer that is not JSON: its bytes as they are, with their content type.

type Route

type Route struct {
	Slice   string
	Match   func(*http.Request) bool
	Handler http.Handler
}

Route is one thing a slice answers itself. A route without a slice is always the Go runtime's.

type SessionRefresh

type SessionRefresh struct {
	Secret []byte
	// Cookie is the session cookie's name, MaxAge its lifetime.
	Cookie string
	MaxAge time.Duration
	// Skip: the request is let in without a session (an open path, or a valid cli-token, which is checked first).
	Skip func(r *http.Request, apiPath string) bool
	// contains filtered or unexported fields
}

SessionRefresh re-signs a page's session past half its age, as the previous runtime's API did on every request it answered, so a person who keeps working never meets the sign-in page.

type Slices

type Slices struct {
	// contains filtered or unexported fields
}

Slices says which slices are on, re-reading state-mode.json at most once a second.

func NewSlices

func NewSlices(dataDir string) *Slices

func (*Slices) On

func (s *Slices) On(name string) bool

Jump to

Keyboard shortcuts

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