Skip to content

Commit

Permalink
Merge pull request #188 from libp2p/remove-manager
Browse files Browse the repository at this point in the history
remove the pstoremanager (will be moved to the Host)
  • Loading branch information
marten-seemann committed Dec 10, 2021
2 parents 584b33d + b5c7bda commit 3e3dd7b
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 772 deletions.
38 changes: 3 additions & 35 deletions p2p/host/peerstore/pstoreds/peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import (
"io"
"time"

"github.com/libp2p/go-libp2p-core/event"

"github.com/libp2p/go-libp2p-peerstore/pstoremanager"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
pstore "github.com/libp2p/go-libp2p-peerstore"
Expand All @@ -27,13 +23,6 @@ type Options struct {
// MaxProtocols is the maximum number of protocols we store for one peer.
MaxProtocols int

// The EventBus that is used to subscribe to EvtPeerConnectednessChanged events.
// This allows the automatic clean up when a peer disconnect.
// This configuration option is optional. If no EventBus is set, it's the callers
// responsibility to call RemovePeer to ensure that memory consumption of the
// peerstore doesn't grow unboundedly.
EventBus event.Bus

// 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
Expand Down Expand Up @@ -71,17 +60,12 @@ type pstoreds struct {
*dsAddrBook
*dsProtoBook
*dsPeerMetadata

manager *pstoremanager.PeerstoreManager
}

var _ peerstore.Peerstore = &pstoreds{}

// NewPeerstore creates a peerstore backed by the provided persistent datastore.
// It is recommended to construct the peerstore with an event bus, using the WithEventBus option.
// In that case, the peerstore will automatically perform cleanups when a peer disconnects
// (see the pstoremanager package for details).
// If constructed without an event bus, it's the caller's responsibility to call RemovePeer to ensure
// It's the caller's responsibility to call RemovePeer to ensure
// that memory consumption of the peerstore doesn't grow unboundedly.
func NewPeerstore(ctx context.Context, store ds.Batching, opts Options) (*pstoreds, error) {
addrBook, err := NewAddrBook(ctx, store, opts)
Expand All @@ -104,22 +88,13 @@ func NewPeerstore(ctx context.Context, store ds.Batching, opts Options) (*pstore
return nil, err
}

ps := &pstoreds{
return &pstoreds{
Metrics: pstore.NewMetrics(),
dsKeyBook: keyBook,
dsAddrBook: addrBook,
dsPeerMetadata: peerMetadata,
dsProtoBook: protoBook,
}
if opts.EventBus != nil {
manager, err := pstoremanager.NewPeerstoreManager(ps, opts.EventBus)
if err != nil {
ps.Close()
return nil, err
}
ps.manager = manager
}
return ps, nil
}, nil
}

// uniquePeerIds extracts and returns unique peer IDs from database keys.
Expand Down Expand Up @@ -156,10 +131,6 @@ func uniquePeerIds(ds ds.Datastore, prefix ds.Key, extractor func(result query.R
return ids, nil
}

func (ps *pstoreds) Start() {
ps.manager.Start()
}

func (ps *pstoreds) Close() (err error) {
var errs []error
weakClose := func(name string, c interface{}) {
Expand All @@ -169,9 +140,6 @@ func (ps *pstoreds) Close() (err error) {
}
}
}
if ps.manager != nil {
weakClose("manager", ps.manager)
}
weakClose("keybook", ps.dsKeyBook)
weakClose("addressbook", ps.dsAddrBook)
weakClose("protobook", ps.dsProtoBook)
Expand Down

0 comments on commit 3e3dd7b

Please sign in to comment.