MCPcopy
hub / github.com/ipfs/kubo / NewNode

Function NewNode

core/builder.go:61–147  ·  view source on GitHub ↗

Alias for compatibility until we properly refactor the constructor interface NewNode constructs and returns an IpfsNode using the given cfg.

(ctx context.Context, cfg *BuildCfg)

Source from the content-addressed store, hash-verified

59
60// NewNode constructs and returns an IpfsNode using the given cfg.
61func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
62 // save this context as the "lifetime" ctx.
63 lctx := ctx
64
65 // derive a new context that ignores cancellations from the lifetime ctx.
66 ctx, cancel := context.WithCancel(valueContext{ctx})
67
68 // add a metrics scope.
69 ctx = metrics.CtxScope(ctx, "ipfs")
70
71 n := &IpfsNode{
72 ctx: ctx,
73 }
74
75 opts := []fx.Option{
76 node.IPFS(ctx, cfg),
77 fx.NopLogger,
78 }
79 for _, optFunc := range fxOptionFuncs {
80 var err error
81 opts, err = optFunc(FXNodeInfo{FXOptions: opts})
82 if err != nil {
83 cancel()
84 return nil, fmt.Errorf("building fx opts: %w", err)
85 }
86 }
87 //nolint:staticcheck // https://github.com/ipfs/kubo/pull/9423#issuecomment-1341038770
88 opts = append(opts, fx.Extract(n))
89
90 app := fx.New(opts...)
91
92 var once sync.Once
93 var stopErr error
94 n.stop = func() error {
95 once.Do(func() {
96 // Bound app.Stop with a deadline so an FX OnStop hook that
97 // never returns cannot hang the daemon. ShutdownTimeout==0
98 // opts out of the cap entirely and restores the legacy
99 // behavior of waiting forever for hooks to complete. The
100 // daemon's watchdog in cmd/ipfs/kubo/daemon.go fires at the
101 // same deadline and is the unconditional os.Exit fallback.
102 stopCtx := context.Background()
103 if cfg.ShutdownTimeout > 0 {
104 var stopCancel context.CancelFunc
105 stopCtx, stopCancel = context.WithTimeout(stopCtx, cfg.ShutdownTimeout)
106 defer stopCancel()
107 }
108 stopErr = app.Stop(stopCtx)
109 if stopErr != nil {
110 log.Errorf("failure on stop: %v", stopErr)
111 }
112 // Cancel the context _after_ the app has stopped.
113 cancel()
114 })
115 return stopErr
116 }
117 n.IsOnline = cfg.Online
118

Callers 15

makeNodeFunction · 0.92
newNodeWithMockNamesysFunction · 0.92
MakeAPISwarmMethod · 0.92
NewMockNodeFunction · 0.92
MockCmdsCtxFunction · 0.92
MockPublicNodeFunction · 0.92
TestAddMultipleGCLiveFunction · 0.92
TestAddGCLiveFunction · 0.92
testAddWPosInfoFunction · 0.92
DirectAddCatFunction · 0.92

Calls 8

BootstrapMethod · 0.95
IPFSFunction · 0.92
logAndUnwrapFxErrorFunction · 0.85
DoMethod · 0.80
DoneMethod · 0.65
ErrMethod · 0.65
StartMethod · 0.65
ErrorMethod · 0.45

Tested by 15

makeNodeFunction · 0.74
newNodeWithMockNamesysFunction · 0.74
MakeAPISwarmMethod · 0.74
TestAddMultipleGCLiveFunction · 0.74
TestAddGCLiveFunction · 0.74
testAddWPosInfoFunction · 0.74
DirectAddCatFunction · 0.74
RunDHTConnectivityFunction · 0.74
benchCatFunction · 0.74
mfsMountFunction · 0.74