node

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package node wraps a libp2p host with DHT and GossipSub.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DHTMode        DHTMode  `yaml:"dht_mode"`
	BootstrapPeers []string `yaml:"bootstrap_peers"`
	EnablePubSub   bool     `yaml:"enable_pubsub"`
}

Config holds node configuration.

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default node configuration.

type DHTMode

type DHTMode int

DHTMode specifies how the DHT operates.

const (
	// DHTModeServer runs the DHT in server mode (serves queries).
	DHTModeServer DHTMode = iota
	// DHTModeClient runs the DHT in client mode (queries only).
	DHTModeClient
	// DHTModeAuto lets the DHT choose based on reachability.
	DHTModeAuto
)

type Node

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

Node wraps a libp2p host with DHT and GossipSub. Topic and subscription maps use sync.Map for lock-free reads during Publish and Subscribe, with per-topic join serialization to avoid blocking unrelated topics.

func New

func New(ctx context.Context, cfg *Config, h host.Host, logger *slog.Logger) (*Node, error)

New creates and starts a new P2P node with DHT and optional GossipSub.

func (*Node) Addrs

func (n *Node) Addrs() []ma.Multiaddr

Addrs returns this node's listen addresses.

func (*Node) Close

func (n *Node) Close() error

Close shuts down the P2P node.

func (*Node) DHT

func (n *Node) DHT() *dht.IpfsDHT

DHT returns the underlying Kademlia DHT.

func (*Node) Host

func (n *Node) Host() host.Host

Host returns the underlying libp2p host.

func (*Node) JoinTopic

func (n *Node) JoinTopic(name string) error

JoinTopic joins a GossipSub topic. Concurrent calls for the same topic name are serialized via per-topic locking, but do not block operations on other topics.

func (*Node) LeaveTopic

func (n *Node) LeaveTopic(name string) error

LeaveTopic cancels the subscription to a topic and closes it, undoing JoinTopic. Leaving a topic that was never joined is not an error. It takes the same per-topic lock as JoinTopic, so a join and a leave of the same topic cannot interleave.

func (*Node) LogDHTStatus

func (n *Node) LogDHTStatus()

LogDHTStatus logs the current DHT routing table and connection state.

func (*Node) PeerID

func (n *Node) PeerID() peer.ID

PeerID returns this node's peer ID.

func (*Node) PubSub

func (n *Node) PubSub() *pubsub.PubSub

PubSub returns the underlying GossipSub instance.

func (*Node) Publish

func (n *Node) Publish(ctx context.Context, topic string, data []byte) error

Publish publishes data to a topic. This is a lock-free read on the topic map.

func (*Node) Subscribe

func (n *Node) Subscribe(topic string) *pubsub.Subscription

Subscribe returns a subscription for a topic. This is a lock-free read on the subscription map.

Jump to

Keyboard shortcuts

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