package irohimport ()// ProtocolHandler handles connections accepted for a single ALPN// (Application-Layer Protocol Negotiation) value. A [Router] dispatches each// incoming connection to the handler registered for its negotiated ALPN.//// Accept is called in its own goroutine for every accepted connection; it should// run for the lifetime of the connection and return when done. A returned error// is logged. A handler must not panic; a panic is recovered and logged, closing// only that connection while the router continues accepting. It is the Go// analog of the Rust ProtocolHandler trait (iroh/src/protocol.rs:228).typeProtocolHandlerinterface {// Accept handles an accepted connection. ctx is cancelled when the router // shuts down.Accept(ctx context.Context, conn *Conn) error}// ProtocolHandlerFunc adapts a function to [ProtocolHandler].typeProtocolHandlerFuncfunc(ctx context.Context, conn *Conn) error// Accept calls f(ctx, conn).func ( ProtocolHandlerFunc) ( context.Context, *Conn) error {return (, )}// AcceptingHandler is an optional interface a [ProtocolHandler] may implement// to intercept an incoming connection before it is converted to a verified// [Conn]. The default behavior is [Accepting.Connection].typeAcceptingHandlerinterface {OnAccepting(ctx context.Context, accepting *Accepting) (*Conn, error)}// ShutdownHandler is an optional interface a [ProtocolHandler] may implement to// run cleanup when its [Router] shuts down. The router calls Shutdown on every// registered handler that implements it before closing the endpoint, giving// handlers a chance to close connections gracefully. It mirrors the Rust// ProtocolHandler::shutdown hook (iroh/src/protocol.rs:284).typeShutdownHandlerinterface {// Shutdown is called once when the router is shutting down.Shutdown(ctx context.Context)}// IncomingFilterOutcome is the decision an [IncomingFilter] returns for an// incoming connection. It mirrors the Rust IncomingFilterOutcome// (iroh/src/protocol.rs).typeIncomingFilterOutcomeintconst (// FilterAccept accepts the connection and dispatches it to a handler.FilterAcceptIncomingFilterOutcome = iota// FilterRetry asks the peer to retry. Router evaluates this outcome before // qng constructs a connection, so it emits a real QUIC Retry packet.FilterRetry// FilterReject refuses the connection.FilterReject// FilterIgnore closes the incoming connection without dispatching it.FilterIgnore)// IncomingFilter decides whether to accept each incoming connection. Router// evaluates FilterRetry at QUIC Initial admission time, before ALPN negotiation;// other outcomes are evaluated in the accept loop after qng has an early// connection. It mirrors the Rust IncomingFilter (iroh/src/protocol.rs).typeIncomingFilterfunc(*Incoming) IncomingFilterOutcome// RouterConfig configures a [Router].typeRouterConfigstruct {// IncomingFilter is consulted for each incoming connection. IncomingFilter IncomingFilter// Logger records handler errors and recovered panics. If nil, // [slog.Default] is used. Logger *slog.Logger}// NewRouter registers every handler ALPN on ep, starts the accept loop, and// returns the running router. The endpoint must not already be listening, so it// must not have an accept loop of its own; see [Endpoint.Accept].//// The router replaces the endpoint's accepted ALPN set with the handler keys.// Passing [WithALPNs] to [Bind] is therefore only useful as a subset of them:// NewRouter returns an error naming any ALPN the endpoint was bound with that// has no handler, rather than negotiating a protocol it cannot dispatch. A// router with more ALPNs than the endpoint was bound with is fine and widens// the set.//// The rule is about what [Bind] was given, not about what the endpoint accepts// at the time. [Endpoint.SetALPNs] documents that it replaces the accepted set,// so a router replacing it again is that contract, not a mistake.//// The handlers map is keyed by exact ALPN string. ALPN values are opaque byte// strings represented as Go strings; printable ASCII protocol names are// conventional, but binary values compare byte-for-byte. NewRouter copies the// map before returning.func ( *Endpoint, map[string]ProtocolHandler, *RouterConfig) (*Router, error) {if := .acquireAcceptOwner(acceptOwnerRouter); != nil {returnnil, } := truedeferfunc() {if { .releaseAcceptOwner(acceptOwnerRouter) } }() = maps.Clone()if := alpnsWithoutHandler(.boundALPNs(), ); len() > 0 {returnnil, fmt.Errorf("iroh: new router: endpoint was bound with %s, which no handler serves", strings.Join(, ", ")) } := slog.Default() := IncomingFilter(nil)if != nil { = .IncomingFilter = .Logger }if == nil { = slog.Default() } := make([]string, 0, len())for := range { = append(, ) } := .sourceAddressValidation()if != nil { .setSourceAddressValidation(func( net.Addr) bool {if != nil && () {returntrue }return (&Incoming{ep: , remote: }) == FilterRetry }) }if := .setALPNs(, acceptOwnerRouter); != nil { .setSourceAddressValidation()returnnil, fmt.Errorf("iroh: new router: %w", ) }for , := range {if , := .(streamListenerHandler); { .l.addr = net.UDPAddrFromAddrPort(.LocalAddr()) } } , := context.WithCancel(context.Background()) := &Router{ep: ,handlers: ,filter: ,restoreSourceValidation: func() { .setSourceAddressValidation() },logger: ,cancel: ,ctx: , } .wg.Add(1)go .acceptLoop() = falsereturn , nil}// Router accepts incoming connections on an [Endpoint] and dispatches each to// the [ProtocolHandler] registered for its negotiated ALPN. Start one with// [NewRouter]; stop it with [Router.Shutdown]. It is the Go analog of the Rust// Router (iroh/src/protocol.rs:97).//// Dispatch is by exact ALPN string. One goroutine runs the accept loop; each// accepted connection is handled in a child goroutine with a context derived// from the router's. A panic in a handler goroutine is recovered and logged,// and closes only that connection; the accept loop continues. As with// [Endpoint.Accept], when too many connections are still handshaking or in// [EndpointHooks.AfterHandshake] or [AcceptingHandler.OnAccepting], the oldest// is abandoned to make room for a new one.typeRouterstruct { ep *Endpoint handlers map[string]ProtocolHandler filter IncomingFilter restoreSourceValidation func() logger *slog.Logger ctx context.Context cancel context.CancelFunc wg sync.WaitGroup mu sync.Mutex shutdown bool}// Endpoint returns the endpoint the router accepts on.func ( *Router) () *Endpoint { return .ep }// IsShutdown reports whether the router has been shut down.func ( *Router) () bool { .mu.Lock()defer .mu.Unlock()return .shutdown}// acceptLoop accepts connections until ctx is cancelled or the endpoint closes.// Each connection is dispatched in a child goroutine.func ( *Router) ( context.Context) {defer .wg.Done()for {select {case<-.Done():returndefault: } , := .ep.acceptIncoming()if != nil {// A cancelled context or a closed endpoint ends the loop cleanly.if .Err() != nil || errors.Is(, ErrEndpointClosed) {return }// A failed accept (e.g. a peer aborting the handshake) is logged and // the loop continues. The endpoint surfaces a hard close via the // checks above. .logger.Warn("router: accept failed", "err", )continue }if .filter != nil {switch .filter() {caseFilterAccept:caseFilterRetry: .Ignore()continuecaseFilterReject: .Refuse()continuecaseFilterIgnore: .Ignore()continue } } , := .Accept()if != nil { .logger.Warn("router: incoming accept failed", "err", )continue } .wg.Add(1)gofunc( *Accepting) {defer .wg.Done()varstringdeferfunc() {if := recover(); != nil { .qc.CloseWithError(0, "handler panic") .logger.Error("router: handler panicked", "alpn", , "panic", ) } }() , := .ep.admissions.admit(, .qc)defer ()varerror , = .ALPN()if != nil {if () == nil && !handlerShutdownErr(, ) { .logger.Warn("router: accepting ALPN failed", "err", ) }return } , := .handlers[]if ! { .logger.Warn("router: no handler for ALPN", "alpn", ) .qc.CloseWithError(0, "unsupported ALPN")return }var *Connif , := .(AcceptingHandler); { , = .OnAccepting(, ) } else { , = .Connection() }if () != nil {return }if != nil { .logger.Warn("router: on accepting failed", "alpn", , "err", )return }if := .Accept(, ); != nil && !handlerShutdownErr(, ) { .logger.Warn("router: handler returned error", "alpn", .ALPN(), "err", ) } }() }}func handlerShutdownErr( context.Context, error) bool {if .Err() != nil && errors.Is(, .Err()) {returntrue }returnerrors.Is(, net.ErrClosed)}// Shutdown stops the router: it cancels the accept loop and all handler// contexts, calls Shutdown on every registered handler that implements// [ShutdownHandler], closes the endpoint, and waits for the accept loop and// handler goroutines to finish or ctx to be done. It is idempotent and the Go// analog of the Rust Router::shutdown (iroh/src/protocol.rs:429).func ( *Router) ( context.Context) error { .mu.Lock()if .shutdown { .mu.Unlock()returnnil } .shutdown = true .mu.Unlock()defer .ep.releaseAcceptOwner(acceptOwnerRouter)if .restoreSourceValidation != nil { .restoreSourceValidation() }// Stop accepting and cancel handler contexts. .cancel()// Give handlers a chance to close connections gracefully before the endpoint // force-closes them. Rust awaits all protocol shutdown futures // concurrently; do the same so one slow protocol does not block the rest.varsync.WaitGroupfor , := range .handlers {if , := .(ShutdownHandler); { .Add(1)gofunc( ShutdownHandler) {defer .Done() .Shutdown() }() } } := make(chanstruct{})gofunc() { .Wait()close() }()select {case<-:case<-.Done():return .Err() } := .ep.Shutdown()// Wait for the accept loop and handler goroutines, bounded by ctx. := make(chanstruct{})gofunc() { .wg.Wait()close() }()select {case<-:case<-.Done():return .Err() }return}// alpnsWithoutHandler returns the ALPNs in alpns that handlers does not cover,// sorted and quoted for an error message. An ALPN with no handler would still// negotiate, then reach an accept loop with nothing to dispatch it to.func alpnsWithoutHandler( []string, map[string]ProtocolHandler) []string {var []stringfor , := range {if , := []; ! { = append(, strconv.Quote()) } }slices.Sort()returnslices.Compact()}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.