relay

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: MIT Imports: 11 Imported by: 12 modules, 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 provides the public configuration types for iroh relay servers: relay URLs grouped into a Map, per-relay Config, and the Mode selecting which relays an endpoint uses.

It is the public surface of the Rust crate iroh-relay (the client connection and wire protocol live in internal packages).

The Go API is not stable before v1 and may change in any v0 release.

Index

Examples

Constants

View Source
const DefaultQUICPort = 7842

DefaultQUICPort is the default port for relay QUIC address discovery.

Variables

View Source
var ErrNoRelays = errors.New("relay: no reachable relay in map")

ErrNoRelays is returned by Map.Nearest when the map has no relays or none could be reached.

Functions

This section is empty.

Types

type Config

type Config struct {
	// URL is the relay server URL.
	URL netaddr.RelayURL
	// QUIC, if non-nil, enables QUIC address discovery via this relay.
	QUIC *QUICConfig
	// AuthToken, if non-empty, is sent to authenticate with the relay.
	AuthToken string
}

Config is the configuration for a single relay server.

func NewConfig

func NewConfig(url netaddr.RelayURL, quic *QUICConfig) Config

NewConfig returns a Config for url with the given optional QUIC config.

func (Config) MarshalText

func (c Config) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler using the relay URL string.

func (Config) String

func (c Config) String() string

func (Config) WithAuthToken

func (c Config) WithAuthToken(token string) Config

WithAuthToken returns a copy of c with the auth token set.

type Map

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

Map is a set of relay servers keyed by URL.

The zero Map is empty and ready to use. A Map is safe for concurrent use and must not be copied after first use.

func DefaultMap

func DefaultMap() *Map

DefaultMap returns the number0 production relay map.

func MapFromURLs

func MapFromURLs(urls ...netaddr.RelayURL) *Map

MapFromURLs builds a Map from relay URLs, each with a default config that enables QUIC address discovery on DefaultQUICPort. net_report only probes relays whose config has a QUIC section, so a nil default would disable address discovery for every URL-built map, including DefaultMap. A relay without QAD just fails the probe, which stays latency-only.

func NewMap

func NewMap(configs ...Config) *Map

NewMap builds a Map from the given relay configs.

func StagingMap

func StagingMap() *Map

StagingMap returns the number0 staging relay map.

func (*Map) Clone

func (m *Map) Clone() *Map

Clone returns a deep copy of the map.

func (*Map) Configs

func (m *Map) Configs() []Config

Configs returns the relay configs in URL-sorted order.

func (*Map) Contains

func (m *Map) Contains(url netaddr.RelayURL) bool

Contains reports whether url is in the map.

func (*Map) Get

func (m *Map) Get(url netaddr.RelayURL) (Config, bool)

Get returns the config for url and whether it is present.

func (*Map) Insert

func (m *Map) Insert(c Config) (Config, bool)

Insert adds or replaces the config for its URL, returning the previous config and whether one was present.

func (*Map) IsEmpty

func (m *Map) IsEmpty() bool

IsEmpty reports whether the map has no relays.

func (*Map) Len

func (m *Map) Len() int

Len returns the number of relays.

func (*Map) Nearest

func (m *Map) Nearest(ctx context.Context, prober Prober) (netaddr.RelayURL, error)

Nearest probes the relays in m and returns the URL of the lowest-latency reachable relay. It returns ErrNoRelays if the map is empty or no relay could be reached. If prober is nil, HTTPConnectProber is used.

Nearest is the seam a ticket minter (e.g. ccl's transfer layer) calls to pick a home relay close to the local machine instead of an arbitrary one.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/tmc/go-iroh/netaddr"
	"github.com/tmc/go-iroh/relay"
)

func main() {
	near, err := netaddr.ParseRelayURL("https://near.example")
	if err != nil {
		panic(err)
	}
	far, err := netaddr.ParseRelayURL("https://far.example")
	if err != nil {
		panic(err)
	}
	m := relay.MapFromURLs(far, near)
	got, err := m.Nearest(context.Background(), func(_ context.Context, u netaddr.RelayURL) (time.Duration, error) {
		if u.Equal(near) {
			return time.Millisecond, nil
		}
		return 10 * time.Millisecond, nil
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(got)
}
Output:
https://near.example/

func (*Map) PreferNearest

func (m *Map) PreferNearest(ctx context.Context, prober Prober) (*Map, error)

PreferNearest returns a Map containing only the lowest-latency reachable relay in m, using prober (or HTTPConnectProber if nil). It is a convenience for wiring nearest-relay selection into a Mode:

m, err := relay.DefaultMap().PreferNearest(ctx, nil)
if err == nil {
	mode = relay.ModeCustom(m)
}

On error (empty map or all relays unreachable) it returns the error and a nil map so the caller can fall back to the full set.

func (*Map) Remove

func (m *Map) Remove(url netaddr.RelayURL) (Config, bool)

Remove deletes the config for url, returning it and whether it was present.

func (*Map) String

func (m *Map) String() string

func (*Map) URLs

func (m *Map) URLs() []netaddr.RelayURL

URLs returns the relay URLs in sorted order.

type Mode

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

Mode selects which relay servers an endpoint uses.

func ModeCustom

func ModeCustom(m *Map) Mode

ModeCustom uses a custom relay map.

func ModeCustomURLs

func ModeCustomURLs(urls ...netaddr.RelayURL) Mode

ModeCustomURLs uses a custom relay map built from the given URLs.

func ModeDefault

func ModeDefault() Mode

ModeDefault uses the number0 production relay servers.

func ModeDisabled

func ModeDisabled() Mode

ModeDisabled disables relay servers entirely.

func ModeStaging

func ModeStaging() Mode

ModeStaging uses the number0 staging relay servers.

func (Mode) Map

func (m Mode) Map() *Map

Map returns the relay map for this mode.

type Prober

type Prober func(ctx context.Context, url netaddr.RelayURL) (time.Duration, error)

A Prober measures the connect latency to a single relay. It returns the round-trip establishment time and nil on success, or a non-nil error if the relay could not be reached within ctx.

Prober is the seam for latency-aware relay selection: RankByLatency and Map.Nearest call it once per candidate relay, concurrently. The default implementation is HTTPConnectProber; tests inject a deterministic Prober.

func HTTPConnectProber

func HTTPConnectProber(tlsConfig *tls.Config) Prober

HTTPConnectProber returns a Prober that measures the time to establish a TLS connection to a relay's HTTPS endpoint. It reflects the real connect cost a relay client pays, which is dominated by round-trip latency to the relay, and is cheaper than a full net-report probe.

tlsConfig, if non-nil, overrides the TLS configuration (used in tests to skip verification against a local relay). A relay URL without an explicit port uses 443.

type QUICConfig

type QUICConfig struct {
	// Port is the QUIC port on the relay server.
	Port uint16
}

QUICConfig configures relay-based QUIC address discovery.

type RelayLatency

type RelayLatency struct {
	URL     netaddr.RelayURL
	Latency time.Duration
	Err     error
}

RelayLatency pairs a relay URL with its measured connect latency.

Err is non-nil when the relay could not be probed; in that case Latency is not meaningful. RankByLatency sorts reachable relays (Err == nil) ahead of unreachable ones.

func RankByLatency

func RankByLatency(ctx context.Context, m *Map, prober Prober) []RelayLatency

RankByLatency probes every relay in m using prober, concurrently, and returns the results sorted by ascending latency. Reachable relays (Err == nil) sort before unreachable ones; ties and unreachable relays are ordered deterministically by relay URL so selection is reproducible.

If prober is nil, HTTPConnectProber is used. RankByLatency never returns a nil slice for a non-empty map: an unreachable relay appears with its Err set.

Jump to

Keyboard shortcuts

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