Help us shape the future of Go! Take the 2026 Go Developer Survey

router

package
v0.1.0-rc.10 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: Apache-2.0 Imports: 60 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHighWaterMark = 4000
	// TrimShare is the share of its connections a router sheds when it
	// crosses the high watermark: the low watermark it trims down to is
	// the high one less this. libp2p's own default trims from 4000 to 1000,
	// which on a hub whose every connection is a member session is most
	// of the mesh at once.
	TrimShare       = 0.15
	ConnGracePeriod = 1 * time.Minute
)
View Source
const DefaultAuthDeadline = time.Minute

DefaultAuthDeadline is the CLI default for Options.AuthDeadline: a member's first act on a connection is the handshake, which takes well under a second; a DHT query from a member attached elsewhere is done in less.

View Source
const DefaultControlPlaneSyncInterval = 5 * time.Minute

DefaultControlPlaneSyncInterval is how often a router talks to the control plane when nothing happens: the lease is renewed, and /info, /keys and /policies re-read, every five minutes. What has to be quick does not wait for it: a mesh event runs a pass at once, a new router reads /info on its own first pass and handshakes with every listed router, and a lookup that finds nothing re-reads /info (refederateIfStale).

View Source
const (

	// DefaultOverloadRetryAfter is how long a member shed for load stays off
	// this router: long enough for the others to absorb it, short enough
	// that a router that emptied out is used again.
	DefaultOverloadRetryAfter = 5 * time.Minute
)
View Source
const DefaultRelayLimitDuration = time.Hour

DefaultRelayLimitDuration is how long a relayed connection lives unless the operator sets it.

View Source
const DefaultRelayMaxCircuits = 1024

DefaultRelayMaxCircuits is how many relayed connections one peer may hold open through this router at once, as source or as destination. libp2p's own default is 16, sized for a peer that occasionally reaches a friend behind a NAT; a service that is reachable only through the router serves one caller per circuit, and a popular one serves hundreds.

View Source
const DefaultShutdownLeaseTTL = 30 * time.Second

DefaultShutdownLeaseTTL is the CLI default for Options.ShutdownLeaseTTL: about the time a pod takes to come back in place, and a pod's default termination grace period.

Variables

This section is empty.

Functions

func DefaultConnsPerSourceIP

func DefaultConnsPerSourceIP(highWaterMark int) int

DefaultConnsPerSourceIP is the per-source-address cap derived from a connection manager high watermark.

func DefaultLowWaterMark

func DefaultLowWaterMark(highWaterMark int) int

DefaultLowWaterMark is the low watermark for a high one: TrimShare below it, so a trim sheds that share of the connections and no more.

Types

type ByteSize

type ByteSize int64

ByteSize is a flag value that accepts human sizes such as 128MiB.

func (*ByteSize) Set

func (b *ByteSize) Set(s string) error

func (*ByteSize) String

func (b *ByteSize) String() string

func (*ByteSize) Type

func (b *ByteSize) Type() string

type Options

type Options struct {
	ControlPlaneURL string
	ListenAddrs     []string
	ExternalAddrs   []string
	// ControlPlaneSyncInterval is how often the router's one pass over the
	// control plane runs: lease, /info, /keys, /policies, credential expiry.
	ControlPlaneSyncInterval time.Duration
	OIDCProvider             string // For OIDC enrollment
	OIDCToken                string // OIDC JWT token for enrollment
	BootstrapToken           string // Pre-shared bootstrap token for enrollment
	BootstrapTokenPath       string // File path containing pre-shared bootstrap token
	JWTPath                  string // File path containing OIDC JWT token
	KeysDBPath               string // Path to save peer private key
	AllowLoopback            bool
	BiscuitTimeout           time.Duration
	LogVerbose               bool
	LowWaterMark             int
	HighWaterMark            int
	// RelayLimitDuration / RelayLimitData cap each relayed connection; 0 is
	// no limit, so Default() leaves them alone.
	RelayLimitDuration time.Duration
	RelayLimitData     int64
	// RelayMaxReservations is how many members may hold a relay reservation,
	// which is what makes a member reachable through this router. Zero is
	// HighWaterMark: every connected member may be reachable.
	RelayMaxReservations int
	// RelayMaxCircuits is how many relayed connections one peer may hold at
	// once through this router. Zero is DefaultRelayMaxCircuits.
	RelayMaxCircuits int
	// ShutdownLeaseTTL is the lease the router asks for as it stops: how
	// long the control plane keeps listing it, which is the time the router
	// expects to be away. Zero sends no last lease and the entry expires on
	// the control plane's schedule. The CLIs default to
	// DefaultShutdownLeaseTTL.
	ShutdownLeaseTTL time.Duration
	// AuthDeadline is how long an inbound connection may stay open without
	// its peer passing the handshake before the router closes it. Members
	// hold a connection for their session; anyone else (a member attached
	// elsewhere whose DHT client queried here, a scanner) is done with the
	// connection long before, and would otherwise count against the
	// watermarks until a trim. Zero keeps every connection. The CLIs
	// default to DefaultAuthDeadline.
	AuthDeadline time.Duration
	// Labels the router declares at enrollment, the same labels as a
	// node's: the control plane signs them only if the router's role allows
	// them (PolicyRole.allowed_labels) and lists them on /info, where a
	// member selects or prefers routers by placement. Validated with
	// api.ValidateLabels.
	Labels map[string]string
	// RequiredRole restricts enrollment and startup to only accept tokens containing this role.
	RequiredRole string
	// HTTPFallbackHandler, when set, serves ordinary (non-WebSocket-upgrade)
	// HTTP requests arriving on the router's WebSocket listen addrs, letting a
	// single port carry both libp2p and REST traffic (single-port mode).
	HTTPFallbackHandler http.Handler
	// ConnsPerSourceIP is the inbound connection cap per source address,
	// and scales the per-address connection rate limit with it. Zero
	// derives it from HighWaterMark: one address may hold at most a
	// quarter of the router's inbound budget, so filling a router takes
	// at least four addresses. libp2p's own default is 8, sized for a
	// public DHT where one IP is one peer; members of a mesh share
	// addresses (pods behind a node's SNAT, a cluster behind a NAT, an
	// office), and the handshake, not this cap, is what keeps strangers
	// out.
	ConnsPerSourceIP int
	// AllowInsecureControlPlane accepts a plaintext http:// ControlPlaneURL
	// to a non-loopback host. Off by default: whoever answers that URL is
	// the trust root.
	AllowInsecureControlPlane bool
	// MetricsAddr, when set, serves /metrics, /healthz and /readyz on a
	// plain HTTP listener separate from the libp2p ports. Off by default:
	// the listener is unauthenticated, so the operator names where it binds.
	MetricsAddr string
}

Options holds configuration details for the agentmesh-router.

func (*Options) Default

func (o *Options) Default()

Default sets default values for options.

func (*Options) Validate

func (o *Options) Validate() error

Validate ensures options are valid.

type Router

type Router struct {
	Host       host.Host
	DHT        *dht.IpfsDHT
	PubSub     *pubsub.PubSub
	EventTopic *pubsub.Topic
	// contains filtered or unexported fields
}

Router represents a libp2p bootstrap/relay node.

func NewRouter

func NewRouter(ctx context.Context, config Options) (*Router, error)

NewRouter initializes the router.

func (*Router) AdmittedUntil

func (r *Router) AdmittedUntil(p peer.ID) (until time.Time, admitted bool)

AdmittedUntil reports when this router's admission of p lapses: the expiry of the biscuit p last presented on the auth handshake. A peer that never authenticated, or whose admission has lapsed, is not admitted.

func (*Router) Close

func (r *Router) Close() error

Close closes the underlying p2p host and keyring db.

func (*Router) HandleAuthHandshake

func (r *Router) HandleAuthHandshake(s network.Stream)

HandleAuthHandshake processes incoming auth connections. It is part of mutual auth: 1. Receives client's Biscuit. 2. Verifies it against CP public keys. 3. Responds with success and the router's own Biscuit.

func (*Router) RefreshEnrollment

func (r *Router) RefreshEnrollment(ctx context.Context) error

RefreshEnrollment trades the expiring biscuit token for a new one using a cryptographic challenge.

func (*Router) Start

func (r *Router) Start() (retErr error)

Start performs enrollment, syncs keys, launches libp2p host, and starts tasks.

Jump to

Keyboard shortcuts

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