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 ¶
- func ConnContext(ctx context.Context, c net.Conn) context.Context
- func Encode(v any) []byte
- type BodyRoute
- type Config
- type Gate
- func (f *Gate) Add(routes ...Route)
- func (f *Gate) AsBodyRoutes(routes ...BodyRoute) []Route
- func (f *Gate) AsRoutes(routes ...JSONRoute) []Route
- func (f *Gate) CloseProxied(slice string) int
- func (f *Gate) HandOverSockets(slice string, paths ...string)
- func (f *Gate) LogCounts()
- func (f *Gate) OnSliceChange(slice string, fn func(on bool))
- func (f *Gate) Proxy(w http.ResponseWriter, r *http.Request)
- func (f *Gate) RefreshSessions(s SessionRefresh)
- func (f *Gate) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (f *Gate) SetFallback(h http.Handler)
- func (f *Gate) SliceOn(slice string) bool
- func (f *Gate) Standalone() bool
- func (f *Gate) WatchSlices(ctx context.Context)
- type JSONRoute
- type OldRuntime
- type Raw
- type Route
- type SessionRefresh
- type Slices
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConnContext ¶
ConnContext is for http.Server.ConnContext: it lets the gate find the connection under an upgraded request.
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 (Config) Standalone ¶
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 (*Gate) AsBodyRoutes ¶
AsBodyRoutes turns body routes into gate routes: answered while the slice is on, shadowed while "<slice>-shadow" is.
func (*Gate) AsRoutes ¶
AsRoutes turns JSON routes into gate routes: answered while the slice is on, shadowed while "<slice>-shadow" is.
func (*Gate) CloseProxied ¶
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 ¶
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 ¶
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) SetFallback ¶
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) Standalone ¶
Standalone says whether the gate runs without an old runtime behind it.
func (*Gate) WatchSlices ¶
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.
type Route ¶
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.