gossip

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: 18 Imported by: 4 modules, 3 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 gossip provides Rust-compatible iroh-gossip transport helpers.

The package implements the unidirectional stream framing used by iroh-gossip. Higher-level topic orchestration and discovery live in later layers.

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

Index

Examples

Constants

ALPN is the Rust iroh-gossip application protocol name.

View Source
const (
	// Provenance is the provenance reported on resolved gossip discovery items.
	Provenance = "gossip"
)

Variables

View Source
var DefaultDiscoveryTopic = TopicID(blake3.Sum256([]byte("iroh-gossip-discovery-v1")))

DefaultDiscoveryTopic is the default topic used by Discovery.

Functions

This section is empty.

Types

type DeliveryScope

type DeliveryScope uint8

DeliveryScope identifies how a received message was delivered.

const (
	// DeliverySwarm reports an epidemic overlay delivery.
	DeliverySwarm DeliveryScope = iota
	// DeliveryNeighbors reports a direct-neighbor delivery.
	DeliveryNeighbors
)

type Discovery

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

Discovery publishes and resolves endpoint addressing information over a gossip topic. The zero value is not usable; create one with New.

Example
package main

import (
	"fmt"

	"github.com/tmc/go-iroh/gossip"
	"github.com/tmc/go-iroh/iroh"
	"github.com/tmc/go-iroh/key"
)

func main() {
	sk, _ := key.GenerateSecretKey()
	discovery := gossip.New(sk.Public().EndpointID())

	var services iroh.AddressLookupServices
	services.AddPublisher(discovery)
	services.AddResolver(discovery)

	fmt.Println(discovery != nil)
}
Output:
true

func New

func New(id key.EndpointID, opts ...Option) *Discovery

New returns a Discovery for id.

func (*Discovery) Publish

func (d *Discovery) Publish(data dns.EndpointData)

Publish advertises data on the gossip topic. It is fire-and-forget and returns immediately, matching iroh.AddressPublisher.

func (*Discovery) Resolve

func (d *Discovery) Resolve(ctx context.Context, id key.EndpointID) iter.Seq2[iroh.Item, error]

Resolve returns the cached item for id, if present, and otherwise waits until ctx or the lookup timeout fires.

func (*Discovery) Start

func (d *Discovery) Start(ctx context.Context) error

Start subscribes to the configured gossip topic until ctx is cancelled.

type Event

type Event struct {
	Kind          EventKind
	Peer          key.EndpointID
	Data          []byte
	Content       []byte
	DeliveredFrom key.EndpointID
	Scope         DeliveryScope
	// Round is the PlumTree delivery round for DeliverySwarm messages.
	// It is zero for direct-neighbor delivery.
	Round uint16
	// Dropped is the number of events lost before a [Lagged] event. It is
	// zero for every other kind.
	Dropped uint64
}

Event is emitted by a subscribed gossip topic.

type EventKind

type EventKind uint8

EventKind identifies a topic event.

const (
	// NeighborUp reports a new direct neighbor for the topic.
	NeighborUp EventKind = iota
	// NeighborDown reports a dropped direct neighbor for the topic.
	NeighborDown
	// Received reports an application gossip message.
	Received
	// PeerData reports metadata propagated by the topic membership overlay.
	PeerData
	// Lagged reports that a slow receiver missed events; [Event.Dropped]
	// says how many. The stream continues after it; the receiver has lost
	// the dropped events, not the subscription.
	//
	// A full subscription queue drops the newest event, not the oldest, so
	// the marker is delivered in the position the loss happened. Rust
	// iroh-gossip rides a tokio broadcast channel and drops the oldest
	// instead; the divergence is deliberate, not a porting mistake.
	Lagged
)

type Gossip

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

Gossip publishes and subscribes to iroh-gossip topics.

Register Gossip.Handler with an iroh Router under ALPN.

func NewGossip

func NewGossip(ep *iroh.Endpoint, opts ...GossipOption) *Gossip

NewGossip returns a Gossip instance for ep.

func (*Gossip) Accept

func (g *Gossip) Accept(ctx context.Context, conn *iroh.Conn) error

Accept handles one incoming iroh-gossip connection.

func (*Gossip) Handler

func (g *Gossip) Handler() iroh.ProtocolHandler

Handler returns the protocol handler for registering this Gossip with an iroh Router.

func (*Gossip) MaxMessageSize

func (g *Gossip) MaxMessageSize() int

MaxMessageSize returns the normalized gossip frame body size.

func (*Gossip) Metrics

func (g *Gossip) Metrics() Metrics

Metrics returns a point-in-time snapshot of gossip counters.

func (*Gossip) Shutdown

func (g *Gossip) Shutdown(ctx context.Context)

Shutdown closes topic subscriptions and open topic send streams.

func (*Gossip) Subscribe

func (g *Gossip) Subscribe(ctx context.Context, topic TopicID, bootstrap []netaddr.EndpointAddr) (*Topic, error)

Subscribe joins topic and returns a local handle for publishing and receiving events. Bootstrap peers are dialed as needed.

func (*Gossip) SubscribeAndJoin

func (g *Gossip) SubscribeAndJoin(ctx context.Context, topic TopicID, bootstrap []netaddr.EndpointAddr) (*Topic, error)

SubscribeAndJoin subscribes to topic and waits until it has a direct neighbor.

func (*Gossip) SubscribeWithOpts

func (g *Gossip) SubscribeWithOpts(ctx context.Context, topic TopicID, opts JoinOptions) (*Topic, error)

SubscribeWithOpts joins topic with opts and returns a local handle for publishing and receiving events.

type GossipOption

type GossipOption func(*Gossip)

GossipOption configures a Gossip instance.

func WithMaxMessageSize

func WithMaxMessageSize(n int) GossipOption

WithMaxMessageSize sets the maximum postcard frame body size. Non-positive values use the Rust default.

type Handler

type Handler struct {
	// MaxMessageSize limits postcard frame bodies. The zero value uses the Rust
	// default.
	MaxMessageSize int
	// Handle is called for each received message. A nil Handle discards
	// messages after validating their frames.
	Handle func(context.Context, key.EndpointID, Message) error
}

Handler receives gossip messages from accepted iroh connections.

func (*Handler) Accept

func (h *Handler) Accept(ctx context.Context, conn *iroh.Conn) error

Accept handles one incoming iroh-gossip connection.

type JoinOptions

type JoinOptions struct {
	// Bootstrap are the peers dialed to join the topic's overlay.
	Bootstrap []netaddr.EndpointAddr

	// SubscriptionCapacity is the number of events the subscription buffers
	// for a receiver that is not reading yet. Zero means 2048.
	SubscriptionCapacity int
}

JoinOptions configures a topic subscription.

type Message

type Message = gossipproto.Message

Message is one topic-scoped gossip protocol message.

type Metrics

type Metrics struct {
	MsgsCtrlSent           uint64
	MsgsCtrlRecv           uint64
	MsgsDataSent           uint64
	MsgsDataRecv           uint64
	MsgsDataSentSize       uint64
	MsgsDataRecvSize       uint64
	MsgsCtrlSentSize       uint64
	MsgsCtrlRecvSize       uint64
	NeighborUp             uint64
	NeighborDown           uint64
	ActorTickMain          uint64
	ActorTickRx            uint64
	ActorTickEndpoint      uint64
	ActorTickDialer        uint64
	ActorTickDialerSuccess uint64
	ActorTickDialerFailure uint64
	ActorTickInEventRx     uint64
	ActorTickTimers        uint64
}

Metrics is a snapshot of gossip counters.

func (Metrics) Snapshot

func (m Metrics) Snapshot() metrics.Snapshot

Snapshot returns m as named counter values for metrics.Registry.

func (Metrics) String

func (m Metrics) String() string

String implements expvar.Var, returning the metrics snapshot as JSON.

type Option

type Option func(*Discovery)

Option configures a Discovery.

func WithGossip

func WithGossip(g *Gossip, topic TopicID, bootstrap []netaddr.EndpointAddr) Option

WithGossip configures the gossip overlay used by a Discovery.

func WithLookupTimeout

func WithLookupTimeout(timeout time.Duration) Option

WithLookupTimeout sets how long Resolve waits after a cache miss.

type PeerID

type PeerID = gossipproto.PeerID

PeerID identifies one peer on the wire.

type Receiver

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

Receiver receives events for a gossip topic.

func (*Receiver) Events

func (r *Receiver) Events() iter.Seq2[Event, error]

Events returns the receiver's topic event stream.

func (*Receiver) IsJoined

func (r *Receiver) IsJoined() bool

IsJoined reports whether the receiver's topic has at least one direct neighbor.

func (*Receiver) Joined

func (r *Receiver) Joined(ctx context.Context) error

Joined waits until the receiver's topic has at least one direct neighbor.

Joined observes the topic's neighbor set, not its event stream, so it can run alongside Receiver.Events without either one taking the other's events.

func (*Receiver) Neighbors

func (r *Receiver) Neighbors() []key.EndpointID

Neighbors returns the receiver's current direct neighbors.

type Sender

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

Sender writes gossip messages to an iroh connection.

func NewSender

func NewSender(conn *iroh.Conn, maxMessageSize int) *Sender

NewSender returns a sender for conn.

func (*Sender) Broadcast

func (s *Sender) Broadcast(ctx context.Context, content []byte) error

Broadcast sends content to the sender's topic epidemic overlay.

func (*Sender) BroadcastNeighbors

func (s *Sender) BroadcastNeighbors(ctx context.Context, content []byte) error

BroadcastNeighbors sends content to the sender's direct topic neighbors.

func (*Sender) Close

func (s *Sender) Close() error

Close closes all open topic streams.

func (*Sender) JoinPeers

func (s *Sender) JoinPeers(ctx context.Context, peers []netaddr.EndpointAddr) error

JoinPeers dials and joins additional peers for the sender's topic.

func (*Sender) Send

func (s *Sender) Send(ctx context.Context, msg Message) error

Send writes msg on the topic stream for msg.Topic.

type Topic

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

Topic is a local subscription to one gossip topic.

func (*Topic) Broadcast

func (t *Topic) Broadcast(ctx context.Context, content []byte) error

Broadcast sends content to the topic's epidemic overlay.

func (*Topic) BroadcastNeighbors

func (t *Topic) BroadcastNeighbors(ctx context.Context, content []byte) error

BroadcastNeighbors sends content to the topic's direct neighbors.

func (*Topic) Close

func (t *Topic) Close() error

Close leaves the topic when this is its last local subscription.

func (*Topic) Events

func (t *Topic) Events() iter.Seq2[Event, error]

Events returns the topic event stream.

The stream starts at Subscribe, not at the call to Events: events that arrive before the first call are buffered, so a caller that joins peers and only then reads still sees the NeighborUp events for that join. The buffer holds JoinOptions.SubscriptionCapacity events; a receiver that falls further behind loses events and sees a single Lagged event marking the gap, but keeps the stream. Only Topic.Close ends it.

func (*Topic) ID

func (t *Topic) ID() TopicID

ID returns the topic ID.

func (*Topic) IsJoined

func (t *Topic) IsJoined() bool

IsJoined reports whether the topic has at least one direct neighbor.

func (*Topic) JoinPeers

func (t *Topic) JoinPeers(ctx context.Context, peers []netaddr.EndpointAddr) error

JoinPeers dials and joins additional peers for this topic.

func (*Topic) Joined

func (t *Topic) Joined(ctx context.Context) error

Joined waits until the topic has at least one direct neighbor. It observes the neighbor set, not the event stream, so it can run alongside Topic.Events.

func (*Topic) Neighbors

func (t *Topic) Neighbors() []key.EndpointID

Neighbors returns the topic's current direct neighbors.

func (*Topic) Split

func (t *Topic) Split() (*Sender, *Receiver)

Split returns separate sender and receiver handles for t.

type TopicID

type TopicID = gossipproto.TopicID

TopicID identifies one gossip topic.

type TopicMessage

type TopicMessage = gossipproto.TopicMessage

TopicMessage is either a membership or broadcast message.

Jump to

Keyboard shortcuts

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