routing

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package routing provides hostname-based reverse proxy routing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RemoveProxyLock

func RemoveProxyLock(dir string) error

RemoveProxyLock removes the proxy lock file.

func SaveProxyLock

func SaveProxyLock(dir string, info ProxyLockInfo) error

SaveProxyLock writes the proxy lock file.

Types

type Lifecycle

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

Lifecycle manages the shared reverse proxy lifecycle.

func NewLifecycle

func NewLifecycle(dir string, desiredPort int) (*Lifecycle, error)

NewLifecycle creates a lifecycle manager for the proxy. If desiredPort is 0, a random available port will be used. If desiredPort is negative, the default port (8080) will be used.

func (*Lifecycle) CA

func (lc *Lifecycle) CA() *proxy.CA

CA returns the CA used for TLS, or nil if TLS is not enabled.

func (*Lifecycle) EnableTLS

func (lc *Lifecycle) EnableTLS() (newCA bool, err error)

EnableTLS enables TLS support by loading or creating a CA. Must be called before EnsureRunning(). Returns true if a new CA was created (caller should print trust instructions).

func (*Lifecycle) EnsureRunning

func (lc *Lifecycle) EnsureRunning() error

EnsureRunning starts the proxy if not already running.

func (*Lifecycle) IsOwner added in v0.8.0

func (lc *Lifecycle) IsOwner() bool

IsOwner reports whether this process started the routing proxy currently serving the port, as opposed to attaching to one another process owns. Callers use this to run work that must happen exactly once per proxy lifetime, such as reconciling the route table against running containers.

func (*Lifecycle) Port

func (lc *Lifecycle) Port() int

Port returns the port the proxy is running on.

func (*Lifecycle) Routes

func (lc *Lifecycle) Routes() *RouteTable

Routes returns the route table.

func (*Lifecycle) ShouldStop

func (lc *Lifecycle) ShouldStop() bool

ShouldStop returns true if there are no more registered agents.

func (*Lifecycle) Stop

func (lc *Lifecycle) Stop(ctx context.Context) error

Stop stops the proxy if this instance owns it.

type ProxyLockInfo

type ProxyLockInfo struct {
	PID       int       `json:"pid"`
	Port      int       `json:"port"`
	StartedAt time.Time `json:"started_at"`
}

ProxyLockInfo holds information about a running proxy.

func LoadProxyLock

func LoadProxyLock(dir string) (*ProxyLockInfo, error)

LoadProxyLock loads the proxy lock file from the given directory. Returns nil, nil if the lock file doesn't exist.

func (*ProxyLockInfo) IsAlive

func (p *ProxyLockInfo) IsAlive() bool

IsAlive checks if the process is still running.

type ProxyServer

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

ProxyServer wraps the reverse proxy with lifecycle management.

func NewProxyServer

func NewProxyServer(routes *RouteTable) *ProxyServer

NewProxyServer creates a new proxy server.

func (*ProxyServer) EnableTLS

func (ps *ProxyServer) EnableTLS(ca *proxy.CA) error

EnableTLS configures TLS support using the given CA. Must be called before Start().

func (*ProxyServer) Port

func (ps *ProxyServer) Port() int

Port returns the port the server is listening on.

func (*ProxyServer) Start

func (ps *ProxyServer) Start(port int) error

Start starts the proxy server on the given port. If TLS is enabled, the server handles both HTTP and HTTPS on the same port.

func (*ProxyServer) Stop

func (ps *ProxyServer) Stop(ctx context.Context) error

Stop gracefully shuts down the proxy server.

type ReverseProxy

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

ReverseProxy routes requests based on Host header to container services.

func NewReverseProxy

func NewReverseProxy(routes *RouteTable) *ReverseProxy

NewReverseProxy creates a reverse proxy with the given route table.

func (*ReverseProxy) ServeHTTP

func (rp *ReverseProxy) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles incoming requests and routes them to backends.

type RouteTable

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

RouteTable manages agent -> endpoint -> host:port mappings.

The table is backed by routes.json in dir, which is shared by every moat process on the host: `moat run` registers a run's published ports, the daemon's API writes on behalf of remote registrations, and the routing proxy reads it to resolve hostnames. Consequently every mutation is a read-modify-write cycle that must be serialized across processes, not just across goroutines — see withFileLock.

func NewRouteTable

func NewRouteTable(dir string) (*RouteTable, error)

NewRouteTable creates or loads a route table from the given directory.

func (*RouteTable) Add

func (rt *RouteTable) Add(agent string, endpoints map[string]string) error

Add registers routes for an agent.

func (*RouteTable) AgentExists

func (rt *RouteTable) AgentExists(agent string) bool

AgentExists returns true if the agent has registered routes. It reloads routes from disk to pick up changes from other processes.

func (*RouteTable) Agents

func (rt *RouteTable) Agents() []string

Agents returns all registered agent names. It reloads routes from disk to pick up changes from other processes.

func (*RouteTable) Endpoints added in v0.7.0

func (rt *RouteTable) Endpoints(agent string) map[string]string

Endpoints returns a copy of an agent's endpoint -> host:port map. It reloads routes from disk to pick up changes from other processes. Returns nil if the agent has no registered routes.

func (*RouteTable) Lookup

func (rt *RouteTable) Lookup(agent, endpoint string) (string, bool)

Lookup returns the host:port for an agent's endpoint. It reloads routes from disk to pick up changes from other processes.

func (*RouteTable) PruneUnreachable added in v0.8.0

func (rt *RouteTable) PruneUnreachable(keep map[string]struct{}) []string

PruneUnreachable removes every agent whose registered endpoints all fail a TCP probe, and returns the names removed. Agents named in keep are never removed, which lets a caller protect routes it has just verified against the container runtime but whose service may not be listening yet.

Reachability, not container enumeration, is the removal test: it cannot misfire on runs belonging to another runtime, another credential profile, or a hand-added entry that is genuinely serving traffic.

func (*RouteTable) Remove

func (rt *RouteTable) Remove(agent string) error

Remove unregisters an agent's routes.

An empty table is persisted as "{}" rather than deleted. Deleting the file used to discard the entries of every still-running agent whenever the map transiently emptied, orphaning long-lived runs for the life of their container (issue #451).

func (*RouteTable) RemoveIfStale added in v0.4.0

func (rt *RouteTable) RemoveIfStale(agent string) bool

RemoveIfStale checks whether any of the agent's registered endpoints are reachable via TCP. If none respond within a short timeout, the route is considered stale (leftover from a crashed or stopped process) and is removed. Returns true if the route was removed, false if the agent is still alive or was not registered.

func (*RouteTable) Snapshot added in v0.7.0

func (rt *RouteTable) Snapshot() map[string]map[string]string

Snapshot returns a deep copy of the full agent -> endpoint -> host:port map. It reloads routes from disk to pick up changes from other processes.

Jump to

Keyboard shortcuts

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