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 ¶
- Constants
- Variables
- type Config
- type Map
- func (m *Map) Clone() *Map
- func (m *Map) Configs() []Config
- func (m *Map) Contains(url netaddr.RelayURL) bool
- func (m *Map) Get(url netaddr.RelayURL) (Config, bool)
- func (m *Map) Insert(c Config) (Config, bool)
- func (m *Map) IsEmpty() bool
- func (m *Map) Len() int
- func (m *Map) Nearest(ctx context.Context, prober Prober) (netaddr.RelayURL, error)
- func (m *Map) PreferNearest(ctx context.Context, prober Prober) (*Map, error)
- func (m *Map) Remove(url netaddr.RelayURL) (Config, bool)
- func (m *Map) String() string
- func (m *Map) URLs() []netaddr.RelayURL
- type Mode
- type Prober
- type QUICConfig
- type RelayLatency
Examples ¶
Constants ¶
const DefaultQUICPort = 7842
DefaultQUICPort is the default port for relay QUIC address discovery.
Variables ¶
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 ¶
MarshalText implements encoding.TextMarshaler using the relay URL string.
func (Config) WithAuthToken ¶
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 MapFromURLs ¶
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 (*Map) Insert ¶
Insert adds or replaces the config for its URL, returning the previous config and whether one was present.
func (*Map) Nearest ¶
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 ¶
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.
type Mode ¶
type Mode struct {
// contains filtered or unexported fields
}
Mode selects which relay servers an endpoint uses.
func ModeCustomURLs ¶
ModeCustomURLs uses a custom relay map built from the given URLs.
type Prober ¶
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 ¶
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 ¶
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.