forked from TRON-US/go-btfs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
91 lines (73 loc) · 2.02 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package core
import (
"context"
"sync"
"time"
"github.com/TRON-US/go-btfs/core/bootstrap"
"github.com/TRON-US/go-btfs/core/node"
"github.com/ipfs/go-metrics-interface"
"go.uber.org/fx"
)
// from https://stackoverflow.com/a/59348871
type valueContext struct {
context.Context
}
func (valueContext) Deadline() (deadline time.Time, ok bool) { return }
func (valueContext) Done() <-chan struct{} { return nil }
func (valueContext) Err() error { return nil }
type BuildCfg = node.BuildCfg // Alias for compatibility until we properly refactor the constructor interface
// NewNode constructs and returns an IpfsNode using the given cfg.
func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
// save this context as the "lifetime" ctx.
lctx := ctx
// derive a new context that ignores cancellations from the lifetime ctx.
ctx, cancel := context.WithCancel(valueContext{ctx})
// add a metrics scope.
ctx = metrics.CtxScope(ctx, "btfs")
n := &IpfsNode{
ctx: ctx,
}
app := fx.New(
node.IPFS(ctx, cfg),
fx.NopLogger,
fx.Extract(n),
)
var once sync.Once
var stopErr error
n.stop = func() error {
once.Do(func() {
stopErr = app.Stop(context.Background())
if stopErr != nil {
log.Error("failure on stop: ", stopErr)
}
// Cancel the context _after_ the app has stopped.
cancel()
})
return stopErr
}
n.IsOnline = cfg.Online
go func() {
// Shut down the application if the lifetime context is canceled.
// NOTE: we _should_ stop the application by calling `Close()`
// on the process. But we currently manage everything with contexts.
select {
case <-lctx.Done():
err := n.stop()
if err != nil {
log.Error("failure on stop: ", err)
}
case <-ctx.Done():
}
}()
if app.Err() != nil {
return nil, app.Err()
}
if err := app.Start(ctx); err != nil {
return nil, err
}
// TODO: How soon will bootstrap move to libp2p?
if !cfg.Online {
return n, nil
}
return n, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
}