From 40f6ecfa7ac59bc9aae3b76136607eaf62a1c4b2 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 | 4 ++-- core/node/libp2p/peerstore.go | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 core/node/libp2p/peerstore.go diff --git a/core/node/groups.go b/core/node/groups.go index ab50b3c0a39a..75607edb72ab 100644 --- a/core/node/groups.go +++ b/core/node/groups.go @@ -161,7 +161,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 +173,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 000000000000..03ad64a6c8a3 --- /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 +}