From 9f1f9584321ab210ca2c7dba427786a8367e716f Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 5 Sep 2019 17:13:30 -0700 Subject: [PATCH] fix: close peerstore on stop fixes #6627 --- core/node/groups.go | 5 ++--- core/node/libp2p/peerstore.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 core/node/libp2p/peerstore.go diff --git a/core/node/groups.go b/core/node/groups.go index ab50b3c0a39..703c269dd05 100644 --- a/core/node/groups.go +++ b/core/node/groups.go @@ -10,7 +10,6 @@ import ( "github.com/ipfs/go-ipfs-config" util "github.com/ipfs/go-ipfs-util" peer "github.com/libp2p/go-libp2p-core/peer" - "github.com/libp2p/go-libp2p-peerstore/pstoremem" pubsub "github.com/libp2p/go-libp2p-pubsub" "github.com/ipfs/go-ipfs/core/node/libp2p" @@ -161,7 +160,7 @@ func Identity(cfg *config.Config) fx.Option { if cfg.Identity.PrivKey == "" { return fx.Options( // No PK (usually in tests) fx.Provide(PeerID(id)), - fx.Provide(pstoremem.NewPeerstore), + fx.Provide(libp2p.Peerstore), ) } @@ -173,7 +172,7 @@ func Identity(cfg *config.Config) fx.Option { return fx.Options( // Full identity fx.Provide(PeerID(id)), fx.Provide(PrivateKey(sk)), - fx.Provide(pstoremem.NewPeerstore), + fx.Provide(libp2p.Peerstore), fx.Invoke(libp2p.PstoreAddSelfKeys), ) diff --git a/core/node/libp2p/peerstore.go b/core/node/libp2p/peerstore.go new file mode 100644 index 00000000000..03ad64a6c8a --- /dev/null +++ b/core/node/libp2p/peerstore.go @@ -0,0 +1,20 @@ +package libp2p + +import ( + "context" + + "github.com/libp2p/go-libp2p-core/peerstore" + "github.com/libp2p/go-libp2p-peerstore/pstoremem" + "go.uber.org/fx" +) + +func Peerstore(lc fx.Lifecycle) peerstore.Peerstore { + pstore := pstoremem.NewPeerstore() + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return pstore.Close() + }, + }) + + return pstore +}