relay

package
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: BSD-2-Clause Imports: 10 Imported by: 1 module, 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 relay is the cluster-side half of the supervisor's push tunnel.

Why it exists. A worker in this cluster cannot reach the Mac that spawned it: the Mac's inbound surface drops every port except a couple that something else already allows, and no amount of cluster-side configuration changes that. The direction that does work is the Mac reaching the cluster, so the tunnel is inverted — the Mac long-polls this relay and the relay holds what arrives in between.

⚠️ THE RELAY DOES NOT VERIFY THE NOTIFICATION TOKEN, AND THAT IS DELIBERATE. The party that mints a credential is the party that verifies it: the supervisor mints a per-worker token and its own receiver compares it against the tokens it issued. A relay that also "validated" would have to hold a copy of every live token, which is a second authority over the same secret and a wider blast radius for nothing. The relay therefore requires only that a token be present — it is the routing key — and queues the body unread. The Mac drops anything whose token its registry does not know, exactly as it does today.

⚠️ AND THAT IS WHY THE PULL SIDE IS AUTHENTICATED INSTEAD. Pushes are unauthenticated by design, so anyone able to reach the pull endpoint could drain them. The tunnel secret is what keeps the queue private, and it is held by the Mac only.

Index

Constants

View Source
const (
	// TokenHeader is the header the A2A SDK's push sender sets. Canonicalised, because Go
	// canonicalises on the server side and a differently-cased constant would silently miss.
	TokenHeader = "A2A-Notification-Token"

	// TunnelSecretHeader authenticates the Mac's pull. Never the token header: a push is a
	// worker's claim, a pull is the operator's channel, and one header for both would let a
	// worker drain the queue.
	//
	//nolint:gosec // G101: this is a header NAME, not a credential. The value it names is
	// supplied by the deployment and never appears in this package.
	TunnelSecretHeader = "X-A2A-Tunnel-Secret"

	// PushPath is what a worker posts to. It mirrors the receiver's own path exactly, so the
	// worker's registration is the only thing that changes for a `k8s` target — not its
	// behaviour, and not the contract it shares with the `docker` arm.
	//
	// ⚠️ IT IS `/a2a/push`, NOT `/relay/push`, AND THE DIFFERENCE IS THE CONTRACT. The task's
	// SC3 requires the endpoint path to be byte-identical in both arms, and a `k8s` worker
	// dials this path verbatim. `/relay/push` would have made the relay's presence visible in
	// the one place the design says it must not be: the worker's own configuration. The relay
	// stays distinguishable by its HOST (`a2a-push-relay.agent.svc.cluster.local`), which is
	// where a reader should look for it.
	PushPath = "/a2a/push"

	// PullPath is what the Mac long-polls.
	PullPath = "/relay/pull"

	// MaxPerToken bounds one worker's queue, matching the receiver's own bound. A status
	// channel is a rolling window of recent facts, not a log: unbounded growth here is a slow
	// leak in a process that lives as long as the Deployment.
	MaxPerToken = 50

	// MaxBodyBytes caps a single push. The SDK's status events are small; a body larger than
	// this is not a status and is refused rather than buffered.
	//
	// ⚠️ 64 KiB, NOT 1 MiB, AND THE REASON IS THE POD'S MEMORY LIMIT. The relay shares its
	// container with the controller's control API, which is limited to 50Mi. A per-item cap
	// multiplied by the item bound is the real ceiling, and at 1 MiB × 500 items it was ~500
	// MiB — so roughly fifty one-megabyte pushes from any worker (the exact party that can
	// reach this deliberately unauthenticated endpoint) would OOMKill the controller and take
	// worker management down with it.
	MaxBodyBytes = 64 << 10

	// MaxTokenBytes caps the notification token, which is otherwise as large as the client
	// cares to make it: Go's default MaxHeaderBytes is 1 MiB, so 500 distinct oversized tokens
	// would retain ~500 MiB as map keys while every body stayed inside the byte bound below.
	// A token minted by the supervisor is a short identifier; 256 bytes is generous.
	MaxTokenBytes = 256

	// MaxConcurrentPushes bounds the bytes IN FLIGHT, which the queue bound cannot see: a
	// handler buffers up to MaxBodyBytes before Store.Push applies any limit, so without this
	// an unauthenticated caller opening many connections holds many buffers at once. At
	// 32 × 64 KiB that is ~2 MiB — comfortably inside the 50Mi the relay shares with the
	// control API — and a push beyond it is refused rather than queued.
	MaxConcurrentPushes = 32

	// MaxTotalBytes bounds the whole queue by memory rather than by count, which is the bound
	// that actually matters: a count only bounds memory once the per-item size is bounded too,
	// and the two limits must be checked together or neither is a ceiling.
	MaxTotalBytes = 4 << 20

	// DefaultWait is the long-poll window. Short enough that a lost connection is noticed
	// quickly, long enough that an idle tunnel costs a handful of requests a minute.
	DefaultWait = 25 * time.Second

	// MaxWait bounds what a caller may ask for, so a client cannot pin a goroutine for ever.
	MaxWait = 60 * time.Second
)

Variables

This section is empty.

Functions

func PullHandler

func PullHandler(store *Store, secret string) http.Handler

PullHandler serves the Mac's long-poll.

func PushHandler

func PushHandler(store *Store) http.Handler

PushHandler accepts one worker push.

func ResolveSettings

func ResolveSettings(ctx context.Context, listen string, secret string) (string, string, error)

ResolveSettings validates the relay's two deployment settings together and returns them trimmed.

The relay is optional, and its two settings are required together: a listener with no secret would serve a pull endpoint that anyone who can reach it could drain. Refused at startup, rather than left to fail on the first pull.

⚠️ THE SECRET IS TRIMMED, NOT MERELY CHECKED FOR EMPTINESS. `RELAY_SECRET=" "` is a secret of one space: non-empty, so an emptiness check accepts it, and trivially guessable. Trimming makes a misconfigured environment stop at startup instead of serving a credential anyone can present. A whitespace-only listen is likewise trimmed to empty, which disables the relay — the same reading the setting's own usage gives "empty".

It lives here rather than in the caller so the guard is covered by this package's suite: the controller's own test file builds the binary and exercises no flag combination at all, so a regression in the guard would ship silently — and the guard is what stands between a listener and an unauthenticated pull endpoint.

Types

type Item

type Item struct {
	Token string          `json:"token"`
	Body  json.RawMessage `json:"body"`
}

Item is one queued push: the token it was routed by, and the body the worker sent.

type Store

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

Store is the per-token queue the relay fills and the Mac drains.

⚠️ A SINGLE NOTIFY CHANNEL, NOT ONE PER TOKEN. The Mac pulls everything at once and dispatches by token itself, so a per-token signal would only add bookkeeping the consumer does not use.

func NewStore

func NewStore(maxTotal int) *Store

NewStore builds a store bounded by the total number of queued items.

⚠️ THE TOTAL BOUND IS THE ONE THAT MATTERS. A per-token bound alone is not a bound at all: tokens are minted per spawn, so a stream of new tokens would grow the map without ever tripping any single token's cap.

func (*Store) Drain

func (s *Store) Drain() []Item

Drain removes and returns everything queued.

func (*Store) Push

func (s *Store) Push(ctx context.Context, token string, body json.RawMessage) error

Push queues one body under its token.

A full store refuses rather than dropping the oldest: a dropped status is invisible, while a refused push reaches the worker's own log, where the failure can be seen.

⚠️ THE CALLER'S CONTEXT IS THREADED INTO EVERY ERROR, never `context.Background()`. The handler already holds the request's, and an error that drops it cannot be traced back to the request that produced it.

func (*Store) Wait

func (s *Store) Wait(ctx context.Context, d time.Duration)

Wait blocks until something is queued or the deadline passes. It returns no value: the caller re-drains, so a spurious wake is harmless and a missed wake costs one poll interval.

Jump to

Keyboard shortcuts

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