kubo/core/builder.go
Gus Eggert 310dca55dd feat: add fx options plugin
This adds a plugin interface that lets the plugin modify the fx
options that are passed to fx when the app is initialized. This means
plugins can inject their own implementations of IPFS interfaces. This
enables granular customization of go-ipfs behavior by plugins, such
as:

- Bitswap with custom filters (e.g. for CID blocking) Custom interface

- implementations such as Pinner or DAGService

- Dynamic configuration of libp2p ...

One downside of this is that we're exposing the entire dependency
graph, init hooks, initialization, etc. to users, so this comes with a
caveat that we reserve the right to make breaking changes to the graph
structure and initialization logic (although this historically happens
rarely). If these things are changed, we should mention them in
release notes and changelogs though, since they could impact users of
this plugin interface.

I'm not particularly fond of DI frameworks (and neither are some of
the folks work on/near go-ipfs), but it seems unlikely that somebody
will rewrite the dependency wiring and lifecycle hooks of go-ipfs, and
add dynamic extension points, so this seems like a palatable
compromise.

There are also problems that we should clean up in how model the
go-ipfs app in fx, such as:

- We make extensive use of nested fx.Options, which fx itself
discourages because it "limits the user's ability to customize their
application". It should be easy to flatten these out into a single
[]fx.Option slice.

- We pass around a list of opaque libp2p opts, which makes it hard to
customize after-the-fact...we should consider naming each of these
opts and providing them to fx as proper dependencies, so that they can
be explicitly overridden.

- We call fx.Invoke() in some places with anonymous functions. We
should instead only pass exported functions to fx.Invoke(), so that
they have exported names, which would make it easier to remove/augment
the invocations that happen when the app is initialized.

These aren't blocking issues, they just make it harder and more
brittle to customize go-ipfs with this plugin.
2022-08-12 17:04:29 -04:00

176 lines
5.1 KiB
Go

package core
import (
"context"
"fmt"
"reflect"
"sync"
"time"
"github.com/ipfs/kubo/core/bootstrap"
"github.com/ipfs/kubo/core/node"
"github.com/ipfs/go-metrics-interface"
"go.uber.org/dig"
"go.uber.org/fx"
)
// FXNodeInfo contains information useful for adding fx options.
type FXNodeInfo struct {
FXOptions []fx.Option
}
// fxOptFunc takes in some info about the IPFS node and returns the full set of fx opts to use.
type fxOptFunc func(FXNodeInfo) ([]fx.Option, error)
var fxOptionFuncs []fxOptFunc
// RegisterFXOptionFunc registers a function that is run before the fx app is initialized.
// Functions are invoked in the order they are registered,
// and the resulting options are passed into the next function's FXNodeInfo.
//
// Note that these are applied globally, by all invocations of NewNode.
// There are multiple places in Kubo that construct nodes, such as:
// - Repo initialization
// - Daemon initialization
// - When running migrations
// - etc.
//
// If your fx options are doing anything sophisticated, you should keep this in mind.
//
// For example, if you plug in a blockservice that disallows non-allowlisted CIDs,
// this may break migrations that fetch migration code over IPFS.
func RegisterFXOptionFunc(optFunc fxOptFunc) {
fxOptionFuncs = append(fxOptionFuncs, optFunc)
}
// 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, "ipfs")
n := &IpfsNode{
ctx: ctx,
}
opts := []fx.Option{
node.IPFS(ctx, cfg),
fx.NopLogger,
}
for _, optFunc := range fxOptionFuncs {
var err error
opts, err = optFunc(FXNodeInfo{FXOptions: opts})
if err != nil {
cancel()
return nil, fmt.Errorf("building fx opts: %w", err)
}
}
opts = append(opts, fx.Extract(n))
app := fx.New(opts...)
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, logAndUnwrapFxError(app.Err())
}
if err := app.Start(ctx); err != nil {
return nil, logAndUnwrapFxError(err)
}
// TODO: How soon will bootstrap move to libp2p?
if !cfg.Online {
return n, nil
}
return n, n.Bootstrap(bootstrap.DefaultBootstrapConfig)
}
// Log the entire `app.Err()` but return only the innermost one to the user
// given the full error can be very long (as it can expose the entire build
// graph in a single string).
//
// The fx.App error exposed through `app.Err()` normally contains un-exported
// errors from its low-level `dig` package:
// * https://github.com/uber-go/dig/blob/5e5a20d/error.go#L82
// These usually wrap themselves in many layers to expose where in the build
// chain did the error happen. Although useful for a developer that needs to
// debug it, it can be very confusing for a user that just wants the IPFS error
// that he can probably fix without being aware of the entire chain.
// Unwrapping everything is not the best solution as there can be useful
// information in the intermediate errors, mainly in the next to last error
// that locates which component is the build error coming from, but it's the
// best we can do at the moment given all errors in dig are private and we
// just have the generic `RootCause` API.
func logAndUnwrapFxError(fxAppErr error) error {
if fxAppErr == nil {
return nil
}
log.Error("constructing the node: ", fxAppErr)
err := fxAppErr
for {
extractedErr := dig.RootCause(err)
// Note that the `RootCause` name is misleading as it just unwraps only
// *one* error layer at a time, so we need to continuously call it.
if !reflect.TypeOf(extractedErr).Comparable() {
// Some internal errors are not comparable (e.g., `dig.errMissingTypes`
// which is a slice) and we can't go further.
break
}
if extractedErr == err {
// We didn't unwrap any new error in the last call, reached the innermost one.
break
}
err = extractedErr
}
return fmt.Errorf("constructing the node (see log for full detail): %w", err)
}