meshdisc

package
v0.6.20 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 15 Imported by: ? 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

Rendered for darwin/amd64

Overview

Package meshdisc provides zero-configuration Bonjour peer discovery, local link inspection, and rendezvous planning for macOS compute meshes.

It uses Apple's Network.framework (nw_listener, nw_advertise_descriptor, nw_browser, nw_txt_record, nw_path_monitor) through purego bindings with CGO_ENABLED=0, combined with local interface and Thunderbolt link probing.

Nodes advertise a Bonjour service type such as "_meshdisc._tcp" with hardware and transport metadata in the TXT record:

adv, err := meshdisc.Advertise(ctx, "_meshdisc._tcp", meshdisc.PeerInfo{
	ID:       "node-a",
	Name:     "mac-studio-a",
	Hardware: "Apple M3 Ultra 256GB",
	TXT:      map[string]string{"gpu": "m3-ultra", "ram_gb": "256"},
})
if err != nil {
	return err
}
defer adv.Close()

Peers browse for the same service type and assemble a deterministic coordinator and rank plan with BuildRendezvousConfig:

browser, err := meshdisc.Browse(ctx, "_meshdisc._tcp")
if err != nil {
	return err
}
defer browser.Close()

cfg, err := meshdisc.BuildRendezvousConfig(browser.Snapshot())

Index

Examples

Constants

View Source
const (
	// DefaultServiceType is the default Bonjour service type for mesh discovery.
	DefaultServiceType = "_meshdisc._tcp"

	// DefaultDomain is the default Bonjour browse and advertise domain.
	DefaultDomain = "local."
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Advertiser

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

Advertiser publishes a Bonjour service using Network.framework's nw_listener, nw_advertise_descriptor, and nw_txt_record.

func Advertise(ctx context.Context, serviceType string, info PeerInfo) (*Advertiser, error)

Advertise starts a Network.framework TCP listener and advertises info as a Bonjour service of type serviceType (for example, "_meshdisc._tcp").

If info.Port is 0, the operating system assigns an ephemeral port, which is reported by Advertiser.Port and propagated into the advertised TXT record. Advertise waits until the listener enters the ready state or ctx expires. Call Advertiser.Close to stop advertising and release the listener.

func (*Advertiser) Close

func (a *Advertiser) Close() error

Close stops advertising and releases the underlying Network.framework listener.

func (*Advertiser) Info

func (a *Advertiser) Info() PeerInfo

Info returns a snapshot of the advertised peer metadata, including the bound port.

func (*Advertiser) Port

func (a *Advertiser) Port() uint16

Port returns the TCP port bound by the underlying nw_listener.

type Browser

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

Browser discovers Bonjour peers using Network.framework's nw_browser.

func Browse

func Browse(ctx context.Context, serviceType string) (*Browser, error)

Browse starts browsing for Bonjour services of type serviceType (for example, "_meshdisc._tcp") on the local network and peer-to-peer links.

Discovered peers are delivered as Event values on Browser.Events and can be queried at any time via Browser.Snapshot. Cancelling ctx or calling Browser.Close stops browsing and closes the events channel.

func (*Browser) Close

func (b *Browser) Close() error

Close stops browsing, closes the events channel, and releases the underlying Network.framework browser.

func (*Browser) Events

func (b *Browser) Events() <-chan Event

Events returns a channel of peer add, update, and remove events. The channel is closed when the browser is closed.

func (*Browser) Snapshot

func (b *Browser) Snapshot() []PeerInfo

Snapshot returns a deterministic, cloned slice of currently discovered peers.

type Event

type Event struct {
	Kind EventKind
	Peer PeerInfo
}

Event reports a peer change observed by Browser.

type EventKind

type EventKind uint8

EventKind classifies a peer discovery event emitted by Browser.

const (
	// EventAdded indicates a peer was discovered.
	EventAdded EventKind = iota + 1
	// EventUpdated indicates an existing peer's TXT record or interfaces changed.
	EventUpdated
	// EventRemoved indicates a previously discovered peer disappeared.
	EventRemoved
)

func (EventKind) String

func (k EventKind) String() string

type LinkInfo

type LinkInfo struct {
	Name         string   `json:"name"`
	Index        int      `json:"index"`
	Kind         LinkKind `json:"kind"`
	NWType       string   `json:"nw_type,omitempty"`
	HardwareAddr string   `json:"hardware_addr,omitempty"`
	MTU          int      `json:"mtu"`
	Up           bool     `json:"up"`
	Running      bool     `json:"running"`
	PointToPoint bool     `json:"point_to_point,omitempty"`
	Thunderbolt  bool     `json:"thunderbolt,omitempty"`
	RDMA         bool     `json:"rdma,omitempty"`
	RDMADevices  []string `json:"rdma_devices,omitempty"`
	IPv4         []string `json:"ipv4,omitempty"`
	IPv6         []string `json:"ipv6,omitempty"`
}

LinkInfo describes a local network interface observed via net.Interfaces and Network.framework's nw_path_monitor.

func LocalLinks() ([]LinkInfo, error)

LocalLinks inspects local network interfaces using net.Interfaces, Network.framework's nw_path_monitor, and a non-invasive /dev RDMA device probe.

Thunderbolt bridges (such as bridge0 and en* Thunderbolt ports) are identified separately from Wi-Fi, wired Ethernet, and loopback interfaces.

type LinkKind

type LinkKind string

LinkKind classifies a local network interface for mesh transport selection.

const (
	LinkThunderbolt LinkKind = "thunderbolt"
	LinkEthernet    LinkKind = "ethernet"
	LinkWiFi        LinkKind = "wifi"
	LinkLoopback    LinkKind = "loopback"
	LinkOther       LinkKind = "other"
)

type PeerInfo

type PeerInfo struct {
	ID          string            `json:"id"`
	Name        string            `json:"name"`
	Host        string            `json:"host,omitempty"`
	Port        uint16            `json:"port,omitempty"`
	Domain      string            `json:"domain,omitempty"`
	Interface   string            `json:"interface,omitempty"`
	TXT         map[string]string `json:"txt,omitempty"`
	Thunderbolt bool              `json:"thunderbolt,omitempty"`
	Hardware    string            `json:"hardware,omitempty"`
}

PeerInfo describes an advertised or discovered mesh peer.

func (PeerInfo) Clone

func (p PeerInfo) Clone() PeerInfo

Clone returns a deep copy of p, including a copy of its TXT map.

type RankAssignment

type RankAssignment struct {
	Rank        int      `json:"rank"`
	Address     string   `json:"address"`
	Coordinator bool     `json:"coordinator,omitempty"`
	Peer        PeerInfo `json:"peer"`
}

RankAssignment maps one discovered peer to a rank in a rendezvous plan.

type RendezvousConfig

type RendezvousConfig struct {
	Coordinator     string           `json:"coordinator"`
	CoordinatorID   string           `json:"coordinator_id"`
	CoordinatorHost string           `json:"coordinator_host,omitempty"`
	CoordinatorPort uint16           `json:"coordinator_port,omitempty"`
	WorldSize       int              `json:"world_size"`
	Backend         string           `json:"backend"`
	ThunderboltMesh bool             `json:"thunderbolt_mesh"`
	Ranks           []RankAssignment `json:"ranks"`
}

RendezvousConfig is a deterministic coordinator and rank plan derived from discovered peers, suitable for bootstrapping compute mesh or x/jaccl clusters.

func BuildRendezvousConfig

func BuildRendezvousConfig(peers []PeerInfo) (RendezvousConfig, error)

BuildRendezvousConfig builds a deterministic coordinator and rank assignment from peers.

Peers are deduplicated by ID (falling back to Name) and ordered deterministically so every node in a cluster computes the same coordinator (rank 0) and rank table independently. If peers specify a numeric "rank" key in TXT, those ranks are honored first; otherwise Thunderbolt-capable peers are ordered before non-Thunderbolt peers, then by ID and Name.

Example
package main

import (
	"fmt"
	"log"

	"github.com/tmc/apple/x/meshdisc"
)

func main() {
	peers := []meshdisc.PeerInfo{
		{
			ID:          "node-b",
			Name:        "mac-studio-b",
			Host:        "169.254.10.2",
			Port:        7778,
			Thunderbolt: true,
			Hardware:    "Apple M3 Ultra 256GB",
		},
		{
			ID:          "node-a",
			Name:        "mac-studio-a",
			Host:        "169.254.10.1",
			Port:        7777,
			Thunderbolt: true,
			Hardware:    "Apple M3 Ultra 256GB",
		},
	}

	cfg, err := meshdisc.BuildRendezvousConfig(peers)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("coordinator=%s backend=%s world_size=%d\n", cfg.Coordinator, cfg.Backend, cfg.WorldSize)
	for _, r := range cfg.Ranks {
		fmt.Printf("rank=%d id=%s addr=%s\n", r.Rank, r.Peer.ID, r.Address)
	}
}
Output:
coordinator=169.254.10.1:7777 backend=jaccl world_size=2
rank=0 id=node-a addr=169.254.10.1:7777
rank=1 id=node-b addr=169.254.10.2:7778

Jump to

Keyboard shortcuts

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