package pstoreds

import "github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoreds"

Deprecated: The database-backed peerstore will be removed from go-libp2p in the future. Use the memory peerstore (pstoremem) instead. For more details see https://github.com/libp2p/go-libp2p/issues/2329 and https://github.com/libp2p/go-libp2p/issues/2355.

Index

Functions

func NewAddrBook

func NewAddrBook(ctx context.Context, store ds.Batching, opts Options) (ab *dsAddrBook, err error)

NewAddrBook initializes a new datastore-backed address book. It serves as a drop-in replacement for pstoremem (memory-backed peerstore), and works with any datastore implementing the ds.Batching interface.

Addresses and peer records are serialized into protobuf, storing one datastore entry per peer, along with metadata to control address expiration. To alleviate disk access and serde overhead, we internally use a read/write-through ARC cache, the size of which is adjustable via Options.CacheSize.

The user has a choice of two GC algorithms:

func NewKeyBook

func NewKeyBook(_ context.Context, store ds.Datastore, _ Options) (*dsKeyBook, error)

func NewPeerMetadata

func NewPeerMetadata(_ context.Context, store ds.Datastore, _ Options) (*dsPeerMetadata, error)

NewPeerMetadata creates a metadata store backed by a persistent db. It uses gob for serialisation.

See `init()` to learn which types are registered by default. Modules wishing to store values of other types will need to `gob.Register()` them explicitly, or else callers will receive runtime errors.

func NewPeerstore

func NewPeerstore(ctx context.Context, store ds.Batching, opts Options) (*pstoreds, error)

NewPeerstore creates a peerstore backed by the provided persistent datastore. It's the caller's responsibility to call RemovePeer to ensure that memory consumption of the peerstore doesn't grow unboundedly.

func NewProtoBook

func NewProtoBook(meta pstore.PeerMetadata, opts ...ProtoBookOption) (*dsProtoBook, error)

Types

type Options

type Options struct {
	// The size of the in-memory cache. A value of 0 or lower disables the cache.
	CacheSize uint

	// MaxProtocols is the maximum number of protocols we store for one peer.
	MaxProtocols int

	// Sweep interval to purge expired addresses from the datastore. If this is a zero value, GC will not run
	// automatically, but it'll be available on demand via explicit calls.
	GCPurgeInterval time.Duration

	// Interval to renew the GC lookahead window. If this is a zero value, lookahead will be disabled and we'll
	// traverse the entire datastore for every purge cycle.
	GCLookaheadInterval time.Duration

	// Initial delay before GC processes start. Intended to give the system breathing room to fully boot
	// before starting GC.
	GCInitialDelay time.Duration

	Clock clock
}

Configuration object for the peerstore.

func DefaultOpts

func DefaultOpts() Options

DefaultOpts returns the default options for a persistent peerstore, with the full-purge GC algorithm:

* Cache size: 1024. * MaxProtocols: 1024. * GC purge interval: 2 hours. * GC lookahead interval: disabled. * GC initial delay: 60 seconds.

type ProtoBookOption

type ProtoBookOption func(*dsProtoBook) error

func WithMaxProtocols

func WithMaxProtocols(num int) ProtoBookOption

Source Files

addr_book.go addr_book_gc.go cache.go cyclic_batch.go deprecate.go keybook.go metadata.go peerstore.go protobook.go

Directories

PathSynopsis
p2p/host/peerstore/pstoreds/pb
Version
v0.41.1 (latest)
Published
Mar 24, 2025
Platform
linux/amd64
Imports
26 packages
Last checked
3 weeks ago

Tools for package owners.