Documentation
¶
Overview ¶
Package proxy is the hub's data plane: an http.Handler that picks the peer a request targets and dials it through that peer's tunnel.
A request names its peer either with the x-tunnel-peer header (works anywhere, no DNS needed) or with a subdomain of a base domain (alice.peers.example.com targets "alice"). A request that names no peer, or names one that is not attached, never reaches a backend: it gets a bare page that says nothing about the hub.
Header routing needs no configuration:
mux.Handle("/", proxy.New(registry))
where registry is a *hub.Registry (or anything else satisfying Peers). Subdomain routing and an observability hook are options:
resolvers, err := proxy.RoutingBoth.Resolvers("peers.example.com")
// handle err — a bad strategy/domain pair fails here, at boot
p := proxy.New(registry,
proxy.WithResolvers(resolvers...),
proxy.WithErrorHook(func(ctx context.Context, reason string) {
errors.Add(ctx, 1, metric.WithAttributes(attribute.String("reason", reason)))
}),
)
Transport encryption is the deployment's job: put a TLS edge, ingress, or mesh in front of the hub, same as its other listeners.
Index ¶
Constants ¶
const ( // ReasonNoPeer is a request that named no peer at all: no route // header, and no host under the base domain. The value stays // "no-header" so existing dashboards keep their series. ReasonNoPeer = "no-header" // ReasonNotAttached is a named peer with no live tunnel. ReasonNotAttached = "not-attached" // ReasonTransport is a live tunnel that failed mid-request. ReasonTransport = "transport" )
The reasons an ErrorHook is called with.
const RouteHeader = "x-tunnel-peer"
RouteHeader is the HTTP header that names the target peer.
Variables ¶
var ErrNoDomain = errors.New("needs a base domain, e.g. peers.example.com")
ErrNoDomain is returned for a subdomain strategy with no base domain to match against.
var ErrUnknownRouting = errors.New("unknown routing strategy")
ErrUnknownRouting is returned for a strategy that is not one of header, subdomain, or both.
var ErrUnusedDomain = errors.New("header routing does not use a base domain; use \"both\" to serve subdomains too")
ErrUnusedDomain is returned when header routing is paired with a base domain. The pair is ambiguous — it reads as "serve subdomains too", which header routing does not do — so it is refused rather than quietly resolved one way.
Functions ¶
This section is empty.
Types ¶
type ErrorHook ¶
ErrorHook observes requests that could not be proxied. It is called with the request context and a low-cardinality reason, which makes it a metric counter's attribute as-is. The response the caller sees is unaffected: the hook only watches.
type Option ¶
type Option func(*Proxy)
Option configures a Proxy.
func WithErrorHook ¶
WithErrorHook registers an observer for requests that could not be proxied. See ErrorHook. The proxy counts them either way; the hook is for anything else the application wants to do with them.
func WithMeterProvider ¶ added in v0.34.0
func WithMeterProvider(mp metric.MeterProvider) Option
WithMeterProvider sets the OTel MeterProvider the data-plane instruments are built from (holt.proxy.requests, .request.duration, .inflight, .errors). Optional: without it the global provider is used, which is a no-op until the application installs an SDK.
func WithResolvers ¶
WithResolvers sets how the target peer is picked: the resolvers are tried in order and the first peer named wins. Build the chain from configuration with Routing.Resolvers — which rejects unusable strategy/domain pairs at boot — and slot in anything else that implements Resolver. Repeatable; later calls append to the chain, and any call replaces the header-routing default.
type Peers ¶
type Peers interface {
// Attached reports whether the peer has a live tunnel right now.
Attached(peer string) bool
// RoundTripper dials the peer through its attached tunnel.
RoundTripper(peer string) http.RoundTripper
}
Peers is the live-tunnel half of the hub the proxy dials through. *hub.Registry satisfies it; a fake satisfies it in tests.
type Proxy ¶
type Proxy struct {
// contains filtered or unexported fields
}
Proxy routes inbound requests to attached peers. Build it with New; the zero value is not usable.
type Resolver ¶
Resolver maps an inbound proxy request to the peer it targets, or "" when the request names none (a bare visit to the proxy, or a host outside the base domain).
func ResolveByHeader ¶
func ResolveByHeader() Resolver
ResolveByHeader routes on the RouteHeader header. It works anywhere — no DNS needed — and is the proxy's default.
func ResolveBySubdomain ¶
ResolveBySubdomain routes on the label(s) in front of domain, so alice.peers.example.com targets "alice" — every peer gets its own hostname, which is what ordinary clients (browsers, webhooks, anything that only takes a URL) can actually use. Without a domain to strip every host would look like a peer, so a blank one is ErrNoDomain instead of a resolver.
type Routing ¶
type Routing string
Routing is the configuration vocabulary for the built-in strategies, as spelled in CLI flags and deployment values. It is turned into behavior in exactly one place — Resolvers — so an invalid strategy or a mismatched domain is an error there, never a proxy that silently drops every request.
const ( RoutingHeader Routing = "header" RoutingSubdomain Routing = "subdomain" RoutingBoth Routing = "both" )
The built-in strategies.
func (Routing) Resolvers ¶
Resolvers builds the resolver chain the configured strategy names — tried in order, first peer named wins — with domain as the base domain for the subdomain strategies. Callers can wrap the error with their own configuration hint; errors.Is against ErrNoDomain, ErrUnusedDomain, and ErrUnknownRouting keeps working through the wrap.