Rewire teardown routines to lifecycles
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Apr 1, 2019 at 18:49 UTC
d0670f22efdabe9b14642a2f8d13eb00acb0f315
3 files changed
+44
-6
core/builder.go
+1
-1
@@ -216,7 +216,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
216
}
217
218
core := fx.Options(
219
- fx.Provide(bserv.New),
219
+ fx.Provide(blockServiceCtor),
220
fx.Provide(dagCtor),
221
fx.Provide(resolver.NewBasicResolver),
222
fx.Provide(pinning),
core/core.go
+2
-2
@@ -684,10 +684,10 @@ func (n *IpfsNode) teardown() error {
684
closers = append(closers, n.Exchange)
685
}
686
687
- if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() {
687
+ if n.Mounts.Ipfs != nil && !n.Mounts.Ipfs.IsActive() { //TODO
688
closers = append(closers, mount.Closer(n.Mounts.Ipfs))
689
}
690
- if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() {
690
+ if n.Mounts.Ipns != nil && !n.Mounts.Ipns.IsActive() { // TODO
691
closers = append(closers, mount.Closer(n.Mounts.Ipns))
692
}
693
core/ncore.go
+41
-3
@@ -159,7 +159,7 @@ func baseBlockstoreCtor(mctx MetricsCtx, repo repo.Repo, cfg *iconfig.Config, bc
159
return
160
}
161
162
-func gcBlockstoreCtor(repo repo.Repo, bb BaseBlocks, cfg *iconfig.Config) (gclocker bstore.GCLocker, gcbs bstore.GCBlockstore, bs bstore.Blockstore, fstore *filestore.Filestore) {
162
+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) {
163
gclocker = bstore.NewGCLocker()
164
gcbs = bstore.NewGCBlockstore(bb, gclocker)
165
@@ -173,6 +173,18 @@ func gcBlockstoreCtor(repo repo.Repo, bb BaseBlocks, cfg *iconfig.Config) (gcloc
173
return
174
}
175
176
+func blockServiceCtor(lc fx.Lifecycle, bs bstore.Blockstore, rem exchange.Interface) bserv.BlockService {
177
+ bsvc := bserv.New(bs, rem)
178
+
179
+ lc.Append(fx.Hook{
180
+ OnStop: func(ctx context.Context) error {
181
+ return bsvc.Close()
182
+ },
183
+ })
184
+
185
+ return bsvc
186
+}
187
+
188
func recordValidator(ps pstore.Peerstore) record.Validator {
189
return record.NamespacedValidator{
190
"pk": record.PublicKeyValidator{},
@@ -431,6 +443,12 @@ func p2pHost(mctx MetricsCtx, lc fx.Lifecycle, params p2pHostIn) (out p2pHostOut
443
out.Host = rhost.Wrap(out.Host, out.Routing)
444
}
445
446
+ lc.Append(fx.Hook{
447
+ OnStop: func(ctx context.Context) error {
448
+ return out.Host.Close()
449
+ },
450
+ })
451
+
452
// TODO: break this up into more DI units
453
// TODO: I'm not a fan of type assertions like this but the
454
// `RoutingOption` system doesn't currently provide access to the
@@ -447,6 +465,12 @@ func p2pHost(mctx MetricsCtx, lc fx.Lifecycle, params p2pHostIn) (out p2pHostOut
465
// that requires a fair amount of work).
466
if dht, ok := out.Routing.(*dht.IpfsDHT); ok {
467
out.IpfsDHT = dht
468
+
469
+ lc.Append(fx.Hook{
470
+ OnStop: func(ctx context.Context) error {
471
+ return out.IpfsDHT.Close()
472
+ },
473
+ })
474
}
475
476
return out, err
@@ -579,7 +603,13 @@ func dagCtor(bs bserv.BlockService) format.DAGService {
603
604
func onlineExchangeCtor(mctx MetricsCtx, lc fx.Lifecycle, host p2phost.Host, rt routing.IpfsRouting, bs bstore.GCBlockstore) exchange.Interface {
605
bitswapNetwork := bsnet.NewFromIpfsHost(host, rt)
582
- return bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs)
606
+ exch := bitswap.New(lifecycleCtx(mctx, lc), bitswapNetwork, bs)
607
+ lc.Append(fx.Hook{
608
+ OnStop: func(ctx context.Context) error {
609
+ return exch.Close()
610
+ },
611
+ })
612
+ return exch
613
}
614
615
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
768
return nil, err
769
}
770
741
- return mfs.NewRoot(ctx, dag, nd, pf)
771
+ root, err := mfs.NewRoot(ctx, dag, nd, pf)
772
+
773
+ lc.Append(fx.Hook{
774
+ OnStop: func(ctx context.Context) error {
775
+ return root.Close()
776
+ },
777
+ })
778
+
779
+ return root, err
780
}
781
782
////////////