Documentation
¶
Overview ¶
Package note provides a high-level API for building P2P applications. Peer wraps discovery, connection management, and optional DHT into one composable unit. NewPeer returns a running peer — no separate Start call.
Trusted mode:
p, err := note.NewPeer("0.0.0.0:9000",
note.WithBootstrap("bootstrap.example.com:9000"),
note.WithHandler("chat/1.0", chatHandler),
)
if err != nil { ... }
defer p.Close()
Verified mode:
kp, _ := identity.LoadOrGenerate("./node.key")
p, err := note.NewVerifiedPeer(kp, "0.0.0.0:9000",
note.WithBootstrap("bootstrap.example.com:9000"),
note.WithHandler("chat/1.0", chatHandler),
)
Index ¶
- func LoadOrGenerateID(path string) (string, error)
- type ConnInfo
- type Handler
- type Message
- type Option
- func WithAdvertiseAddr(addr string) Option
- func WithBootstrap(addrs ...string) Option
- func WithCodec(c codec.Codec) Option
- func WithDHT(cfg ...dht.Config) Option
- func WithHandler(protocol string, handler Handler) Option
- func WithHandlerFactory(factory func(node.Node)) Option
- func WithHandshakeTimeout(d time.Duration) Option
- func WithIdentityPath(path string) Option
- func WithMaxFrameSize(bytes uint32) Option
- func WithMaxInboundPeers(n int) Option
- func WithMaxPeers(n int) Option
- func WithMaxPendingPeers(n int) Option
- func WithPeerConnected(fn func(peerID string)) Option
- func WithPeerDisconnected(fn func(peerID string)) Option
- func WithPingInterval(d time.Duration) Option
- func WithPingMaxMissed(n int) Option
- func WithProtocolFrameSize(protocol string, bytes uint32) Option
- type Peer
- func (p *Peer) Addr() string
- func (p *Peer) Announce(ctx context.Context, key, value []byte) (StoreResult, error)
- func (p *Peer) Broadcast(msg Message) int
- func (p *Peer) Close() error
- func (p *Peer) ConnectionInfo(peerID string) (ConnInfo, bool)
- func (p *Peer) FindProviders(ctx context.Context, key []byte) ([]ProviderRecord, error)
- func (p *Peer) ID() string
- func (p *Peer) Lookup(ctx context.Context, key []byte) ([]dht.NodeInfo, error)
- func (p *Peer) Peers() []string
- func (p *Peer) Send(peerID string, msg Message) (int, error)
- type ProviderRecord
- type StoreResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LoadOrGenerateID ¶
LoadOrGenerateID reads a UUID identity from path, generating and persisting one if absent.
Types ¶
type Message ¶
Message is an outbound P2P message. A struct prevents transposing protocol and type, which are both strings.
type Option ¶
type Option func(*peerConfig)
Option configures a Peer.
func WithAdvertiseAddr ¶
WithAdvertiseAddr sets the address announced to peers. Required when listen addr is not directly routable (NAT, Docker, cloud VMs).
func WithBootstrap ¶
func WithCodec ¶
WithCodec sets the network codec. All peers in the network must use the same codec. Default: JSON.
func WithDHT ¶
WithDHT enables Kademlia DHT. An optional dht.Config tunes bucket size, alpha, and timeouts.
func WithHandler ¶
WithHandler registers a sub-protocol handler. Must be called before Start.
func WithHandlerFactory ¶
WithHandlerFactory registers a factory called with node.Node after static handlers are registered but before Start. Use when the handler needs the node at construction time.
func WithHandshakeTimeout ¶
WithHandshakeTimeout sets the handshake deadline (identity exchange or TLS). Default: 10s.
func WithIdentityPath ¶
WithIdentityPath sets where the trusted-mode UUID identity is persisted. Default: ./<sanitized_addr>.id.
func WithMaxFrameSize ¶
WithMaxFrameSize sets the global transport-level frame size ceiling. Default: 64 KiB. Both sides must accept the same or larger limit; use WithProtocolFrameSize for per-protocol caps.
func WithMaxInboundPeers ¶
WithMaxInboundPeers sets the inbound sub-limit within MaxPeers, reserving capacity for outbound dials. Default: MaxPeers * 2/3.
func WithMaxPeers ¶
func WithMaxPendingPeers ¶
WithMaxPendingPeers caps concurrent in-flight handshakes; bounds TLS CPU cost under connection storms. Default: 32.
func WithPeerConnected ¶
WithPeerConnected sets a callback fired when a peer reaches CONNECTED. DHT routing is seeded before this fires.
func WithPeerDisconnected ¶
func WithPingInterval ¶
WithPingInterval controls liveness ping frequency. Shorter detects failures faster; longer reduces UDP traffic. Default: 1s.
func WithPingMaxMissed ¶
WithPingMaxMissed sets consecutive unanswered pings before eviction. Dead-peer window = PingInterval × n. Default: 3.
func WithProtocolFrameSize ¶
WithProtocolFrameSize sets a per-protocol frame size limit enforced at dispatch time, layered on top of the global ceiling set by WithMaxFrameSize.
type Peer ¶
type Peer struct {
// contains filtered or unexported fields
}
Peer is a P2P network participant. Create with NewPeer or NewVerifiedPeer.
func NewPeer ¶
NewPeer creates and starts a trusted-mode Peer. addr is used for both UDP discovery and TCP connections.
func NewVerifiedPeer ¶
NewVerifiedPeer creates and starts a verified-mode Peer with Ed25519 identity.
func (*Peer) Addr ¶
Addr returns the listen address; reflects OS-assigned port when started with ":0".
func (*Peer) Announce ¶
Announce registers this peer as a provider for key. Requires WithDHT. Attempted==0 means no other nodes exist yet; Replicated<Attempted means partial replication.
func (*Peer) FindProviders ¶
FindProviders returns all nodes that have announced themselves as holders of key. Requires WithDHT.
type ProviderRecord ¶
type ProviderRecord = dht.ProviderRecord
ProviderRecord is a DHT provider entry returned by FindProviders.
type StoreResult ¶
type StoreResult = dht.StoreResult
StoreResult describes the outcome of an Announce operation.
Directories
¶
| Path | Synopsis |
|---|---|
|
demo
|
|
|
cas
command
demo/cas demonstrates a content-addressed distributed storage network built on the note framework.
|
demo/cas demonstrates a content-addressed distributed storage network built on the note framework. |
|
cas/blockstore
Package blockstore defines the storage contract for content-addressed blocks and provides an in-memory implementation.
|
Package blockstore defines the storage contract for content-addressed blocks and provides an in-memory implementation. |
|
cas/chunker
Package chunker splits files into fixed-size content-addressed blocks and produces a Manifest that links them together.
|
Package chunker splits files into fixed-size content-addressed blocks and produces a Manifest that links them together. |
|
chat
command
demo/chat demonstrates a distributed P2P chat application built on the note framework.
|
demo/chat demonstrates a distributed P2P chat application built on the note framework. |
|
gossip
command
demo/gossip demonstrates epidemic broadcast: each node publishes one message and the gossip protocol ensures every node eventually receives every message, with no central coordinator.
|
demo/gossip demonstrates epidemic broadcast: each node publishes one message and the gossip protocol ensures every node eventually receives every message, with no central coordinator. |
|
pkg
|
|
|
identity
Package identity manages Ed25519 keypairs for verified-mode nodes.
|
Package identity manages Ed25519 keypairs for verified-mode nodes. |
|
node/identify
Package identify implements node.Handshaker.
|
Package identify implements node.Handshaker. |
|
p2p
Package p2p defines the shared vocabulary of peer-to-peer communication.
|
Package p2p defines the shared vocabulary of peer-to-peer communication. |
|
transport/tls
Package tlstransport provides a TLS StreamTransport for verified-mode nodes.
|
Package tlstransport provides a TLS StreamTransport for verified-mode nodes. |
