Documentation
¶
Overview ¶
Package chain implements the core routing infrastructure for TNET.
It provides three key abstractions:
Router: top-level entry point that resolves addresses, selects routes via a Chainer, retries on failure, and records telemetry.
Chain: a named sequence of proxy hops (nodes). Each hop selects a node from its group, and the resulting Route carries traffic through every selected node in order.
Transport: bundles a dialer and connector for a single chain node. It handles Dial, Handshake, Connect, and Bind — the four steps needed to move traffic through a proxy hop.
Route traversal ¶
For a chain of N nodes, the first node is reached via Dial → Handshake, and each subsequent node via Connect → Handshake through the previous connection. On failure, connections are cleaned up and nodes are marked so selectors can deprioritize them.
Multiplexing ¶
When a node's transport supports multiplexing, Chain splits the route at that point: nodes before the multiplex-capable node form a sub-route that is copied into the transport, establishing a reusable tunnel for subsequent connections.
Index ¶
- Variables
- func NewChainGroup(chains ...chain.Chainer) *chainGroup
- func NewRoute(opts ...RouteOption) *chainRoute
- func StartNodeProbe(node *chain.Node, cfg *chain.ProbeConfig, log logger.Logger)
- type Chain
- type ChainEntry
- type ChainOption
- type ChainOptions
- type RouteOption
- type RouteOptions
- type Router
- func (r *Router) Bind(ctx context.Context, network, address string, opts ...chain.BindOption) (ln net.Listener, err error)
- func (r *Router) Close() error
- func (r *Router) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (conn net.Conn, err error)
- func (r *Router) Options() *chain.RouterOptions
- type Transport
- func (tr *Transport) Bind(ctx context.Context, conn net.Conn, network, address string, ...) (net.Listener, error)
- func (tr *Transport) Connect(ctx context.Context, conn net.Conn, network, address string) (net.Conn, error)
- func (tr *Transport) Copy() chain.Transporter
- func (tr *Transport) Dial(ctx context.Context, addr string) (net.Conn, error)
- func (tr *Transport) Handshake(ctx context.Context, conn net.Conn) (net.Conn, error)
- func (tr *Transport) Multiplex() bool
- func (tr *Transport) Options() *chain.TransportOptions
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultRoute is a fallback route that dials the target directly // without going through any proxy nodes. DefaultRoute chain.Route = &defaultRoute{} )
var ( // ErrEmptyRoute is returned when the configured chain produces a route // with no nodes. ErrEmptyRoute = errors.New("empty route") )
Functions ¶
func NewChainGroup ¶
NewChainGroup creates a chain group that selects one Chainer from the given list using the configured selector (round-robin by default).
func NewRoute ¶
func NewRoute(opts ...RouteOption) *chainRoute
NewRoute creates a new route with the given options.
func StartNodeProbe ¶ added in v0.28.1
StartNodeProbe starts a background goroutine that periodically probes the node's transport. On success the node's marker is reset; on failure the marker is incremented. cfg may be nil (no-op).
Types ¶
type Chain ¶
type Chain struct {
// contains filtered or unexported fields
}
func NewChain ¶
func NewChain(name string, opts ...ChainOption) *Chain
NewChain creates a new Chain with the given name and options.
func (*Chain) AddHop ¶
AddHop appends a hop to the chain. Hops are traversed in order during route construction.
func (*Chain) Close ¶ added in v0.28.1
Close stops all probe goroutines by cascading to each hop's Close, which in turn cascades to node.Close() for every node.
func (*Chain) Metadata ¶
Metadata returns the chain's metadata. Implements metadata.Metadatable interface.
func (*Chain) Route ¶
func (c *Chain) Route(ctx context.Context, network, address string, opts ...chain.RouteOption) chain.Route
Route builds a route by selecting one node from each hop. If a node supports multiplexing, the route is split — nodes before it form a sub-route that is copied into the transport for reuse.
type ChainEntry ¶ added in v0.28.1
type ChainEntry struct {
// contains filtered or unexported fields
}
ChainEntry wraps a chain.Chainer with an optional matcher and an independent failure marker for chain-level probe tracking. The entry's marker is separate from any node-level markers inside the chain so that probe Reset() cannot undo a node-level failCodes Mark().
func NewChainEntry ¶ added in v0.28.1
func NewChainEntry(c chain.Chainer, m routing.Matcher, probe *chain.ProbeConfig) *ChainEntry
NewChainEntry creates a ChainEntry. matcher may be nil (unconditional catch-all). probe may be nil (no health check for this entry).
func (*ChainEntry) Marker ¶ added in v0.28.1
func (e *ChainEntry) Marker() selector.Marker
Marker implements selector.Markable.
func (*ChainEntry) Route ¶ added in v0.28.1
func (e *ChainEntry) Route(ctx context.Context, network, address string, opts ...chain.RouteOption) chain.Route
Route implements chain.Chainer so the entry can be passed directly to the group selector. The selector's FailFilter reads Marker() from the entry, not from the underlying chain — this keeps probe-driven marking independent of any node-level markers.
type ChainOption ¶
type ChainOption func(*ChainOptions)
func LoggerChainOption ¶
func LoggerChainOption(logger logger.Logger) ChainOption
func MetadataChainOption ¶
func MetadataChainOption(md metadata.Metadata) ChainOption
type RouteOption ¶
type RouteOption func(*RouteOptions)
func ChainRouteOption ¶
func ChainRouteOption(c chain.Chainer) RouteOption
ChainRouteOption sets the parent chain on a route, enabling error tracking and metrics for the chain.
type RouteOptions ¶
type Router ¶ added in v0.0.3
type Router struct {
// contains filtered or unexported fields
}
Router is the top-level routing entry point. It resolves addresses, selects routes through the configured chain, retries on failure, and records telemetry (address events, errors, metrics).
func NewRouter ¶ added in v0.0.3
func NewRouter(opts ...chain.RouterOption) *Router
NewRouter creates a Router with the given options. Defaults: 15s timeout, a logger with kind "router" if none is provided.
func (*Router) Bind ¶ added in v0.0.3
func (r *Router) Bind(ctx context.Context, network, address string, opts ...chain.BindOption) (ln net.Listener, err error)
Bind creates a listener bound to the given address through the configured chain. It retries up to Retries+1 times on failure.
func (*Router) Close ¶ added in v0.28.1
Close releases resources held by the router's chain. A chain that owns background goroutines (e.g. a chainGroup running liveness probes) is closed here so its goroutines stop when the service shuts down or is reloaded.
func (*Router) Dial ¶ added in v0.0.3
func (r *Router) Dial(ctx context.Context, network, address string, opts ...chain.DialOption) (conn net.Conn, err error)
Dial establishes a connection to the target address through the configured chain. It resolves the address, records the dial event, retries up to Retries+1 times, and returns the resulting connection. For UDP networks the returned connection is wrapped as a PacketConn if the underlying transport does not already implement net.PacketConn.
func (*Router) Options ¶ added in v0.0.3
func (r *Router) Options() *chain.RouterOptions
type Transport ¶ added in v0.0.3
type Transport struct {
// contains filtered or unexported fields
}
Transport bundles a dialer and connector for a single chain node. It implements chain.Transporter and handles the four operations in the proxy chain lifecycle: Dial, Handshake, Connect, and Bind.
func NewTransport ¶ added in v0.0.3
NewTransport creates a Transport with the given dialer and connector.
func (*Transport) Copy ¶ added in v0.0.3
func (tr *Transport) Copy() chain.Transporter
Copy returns a shallow copy of the transport. Used by Chain when splitting a route at a multiplex-capable node so the sub-route can be embedded in the transport without sharing state across calls.
func (*Transport) Options ¶ added in v0.0.3
func (tr *Transport) Options() *chain.TransportOptions