Documentation
¶
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 ¶
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 ¶
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 ¶
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 ¶
Close stops browsing, closes the events channel, and releases the underlying Network.framework browser.
type EventKind ¶
type EventKind uint8
EventKind classifies a peer discovery event emitted by Browser.
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 ¶
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.
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.
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