From d0670f22efdabe9b14642a2f8d13eb00acb0f315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Mon, 1 Apr 2019 18:49:36 +0200 Subject: [PATCH] Rewire teardown routines to lifecycles MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit License: MIT Signed-off-by: Ɓukasz Magiera --- core/builder.go | 2 +- core/core.go | 4 ++-- core/ncore.go | 44 +++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/core/builder.go b/core/builder.go index 358e76194..e9cdc8fa3 100644 --- a/core/builder.go +++ b/core/builder.go @@ -216,7 +216,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) { } core := fx.Options( - fx.Provide(bserv.New), + fx.Provide(blockServiceCtor), fx.Provide(dagCtor), fx.Provide(resolver.NewBasicResolver), fx.Provide(pinning), diff --git a/core/core.go b/core/core.go index d71e0a7f5..1a62dc4e4 100644 --- a/core/core.go +++ b/core/core.go @@ -684,10 +684,10 @@ func (n *IpfsNode) teardown() error { closers = append(closers, n.Exchange) } - if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() { + if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() { //TODO closers = append(closers, mount.Closer(n.Mounts.Ipfs)) } - if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() { + if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() { // TODO closers = append(closers, mount.Closer(n.Mounts.Ipns)) } diff --git a/core/ncore.go b/core/ncore.go index 6b8130a57..dc27bebcf 100644 --- a/core/ncore.go +++ b/core/ncore.go @@ -159,7 +159,7 @@ func baseBlockstoreCtor(mctx MetricsCtx, repo repo.Repo, cfg *iconfig.Config, bc return } -func gcBlockstoreCtor(repo repo.Repo, bb BaseBlocks, cfg *iconfig.Config) (gclocker bstore.GCLocker, gcbs bstore.GCBlockstore, bs bstore.Blockstore, fstore *filestore.Filestore) { +func gcBlockstoreCtor(lc fx.Lifecycle, repo repo.Repo, bb BaseBlocks, cfg *iconfig.Config) (gclocker bstore.GCLocker, gcbs bstore.GCBlockstore, bs bstore.Blockstore, fstore *filestore.Filestore) { gclocker = bstore.NewGCLocker() gcbs = bstore.NewGCBlockstore(bb, gclocker) @@ -173,6 +173,18 @@ func gcBlockstoreCtor(repo repo.Repo, bb BaseBlocks, cfg *iconfig.Config) (gcloc return } +func blockServiceCtor(lc fx.Lifecycle, bs bstore.Blockstore, rem exchange.Interface) bserv.BlockService { + bsvc := bserv.New(bs, rem) + + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return bsvc.Close() + }, + }) + + return bsvc +} + func recordValidator(ps pstore.Peerstore) record.Validator { return record.NamespacedValidator{ "pk": record.PublicKeyValidator{}, @@ -431,6 +443,12 @@ func p2pHost(mctx MetricsCtx, lc fx.Lifecycle, params p2pHostIn) (out p2pHostOut out.Host = rhost.Wrap(out.Host, out.Routing) } + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return out.Host.Close() + }, + }) + // TODO: break this up into more DI units // TODO: I'm not a fan of type assertions like this but the // `RoutingOption` system doesn't currently provide access to the @@ -447,6 +465,12 @@ func p2pHost(mctx MetricsCtx, lc fx.Lifecycle, params p2pHostIn) (out p2pHostOut // that requires a fair amount of work). if dht, ok := out.Routing.(*dht.IpfsDHT); ok { out.IpfsDHT = dht + + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return out.IpfsDHT.Close() + }, + }) } return out, err @@ -579,7 +603,13 @@ func dagCtor(bs bserv.BlockService) format.DAGService { func onlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host p2phost.Host, rt routing.IpfsRouting, bs bstore.GCBlockstore) exchange.Interface { bitswapNetwork := bsnet.NewFromIpfsHost(host, rt) - return bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs) + exch := bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs) + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return exch.Close() + }, + }) + return exch } func onlineNamesysCtor(rt routing.IpfsRouting, repo repo.Repo, cfg *iconfig.Config) (namesys.NameSystem, error) { @@ -738,7 +768,15 @@ func files(mctx MetricsCtx, lc fx.Lifecycle, repo repo.Repo, dag format.DAGServi return nil, err } - return mfs.NewRoot(ctx, dag, nd, pf) + root, err := mfs.NewRoot(ctx, dag, nd, pf) + + lc.Append(fx.Hook{ + OnStop: func(ctx context.Context) error { + return root.Close() + }, + }) + + return root, err } ////////////