core: call app.Stop once
Łukasz Magiera committed
May 25, 2019 at 18:28 UTC
7cfb4aa2b443d9fb6e385928b87f1c6debbb9e65
2 files changed
+14
-8
core/builder.go
+12
-4
@@ -2,6 +2,7 @@ package core
2
3
import (
4
"context"
5
+ "sync"
6
7
"github.com/ipfs/go-metrics-interface"
8
"go.uber.org/fx"
@@ -27,19 +28,26 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
28
fx.Extract(n),
29
)
30
31
+ var once sync.Once
32
+ var stopErr error
33
+ n.stop = func() error {
34
+ once.Do(func() {
35
+ stopErr = app.Stop(context.Background())
36
+ })
37
+ return stopErr
38
+ }
39
+ n.IsOnline = cfg.Online
40
+
41
go func() {
42
// Note that some services use contexts to signal shutting down, which is
43
// very suboptimal. This needs to be here until that's addressed somehow
44
<-ctx.Done()
34
- err := app.Stop(context.Background())
45
+ err := n.stop()
46
if err != nil {
47
log.Error("failure on stop: ", err)
48
}
49
}()
50
40
- n.IsOnline = cfg.Online
41
- n.app = app
42
-
51
if app.Err() != nil {
52
return nil, app.Err()
53
}
core/core.go
+2
-4
@@ -13,8 +13,6 @@ import (
13
"context"
14
"io"
15
16
- "go.uber.org/fx"
17
-
16
version "github.com/ipfs/go-ipfs"
17
"github.com/ipfs/go-ipfs/core/bootstrap"
18
"github.com/ipfs/go-ipfs/core/node"
@@ -107,7 +105,7 @@ type IpfsNode struct {
105
Process goprocess.Process
106
ctx context.Context
107
110
- app *fx.App
108
+ stop func() error
109
110
// Flags
111
IsOnline bool `optional:"true"` // Online is set when networking is enabled.
@@ -124,7 +122,7 @@ type Mounts struct {
122
123
// Close calls Close() on the App object
124
func (n *IpfsNode) Close() error {
127
- return n.app.Stop(n.ctx)
125
+ return n.stop()
126
}
127
128
// Context returns the IpfsNode context