Skip to content

Commit

Permalink
feat: add fx options plugin
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
guseggert committed Jun 3, 2022
1 parent 2e170a4 commit 3a0e18c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
23 changes: 19 additions & 4 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import (
"go.uber.org/fx"
)

type fxOptFunc func([]fx.Option) []fx.Option

var fxOptionFuncs []fxOptFunc

// RegisterFXOptionFunc registers a function that is run before the fx app is initialized,
// with the list of all fx options that will be passed during initialization, and that
// returns a new list of all fx options.
func RegisterFXOptionFunc(optFunc fxOptFunc) {
fxOptionFuncs = append(fxOptionFuncs, optFunc)
}

// from https://stackoverflow.com/a/59348871
type valueContext struct {
context.Context
Expand All @@ -41,12 +52,16 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
ctx: ctx,
}

app := fx.New(
opts := []fx.Option{
node.IPFS(ctx, cfg),

fx.NopLogger,
fx.Extract(n),
)
}
for _, optFunc := range fxOptionFuncs {
opts = optFunc(opts)
}
opts = append(opts, fx.Extract(n))

app := fx.New(opts...)

var once sync.Once
var stopErr error
Expand Down
16 changes: 16 additions & 0 deletions plugin/fx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package plugin

import (
"go.uber.org/fx"
)

// PluginFx can be used to customize the fx options passed to the go-ipfs app when it is initialized.
//
// This is invasive and depends on internal details such as the structure of the dependency graph,
// so breaking changes might occur between releases.
// So it's recommended to keep this as simple as possible, and stick to overriding interfaces
// with fx.Replace() or fx.Decorate().
type PluginFx interface {
Plugin
Options(opts []fx.Option) []fx.Option
}
13 changes: 12 additions & 1 deletion plugin/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ func (loader *PluginLoader) Inject() error {

for _, pl := range loader.plugins {
if pl, ok := pl.(plugin.PluginIPLD); ok {

err := injectIPLDPlugin(pl)
if err != nil {
loader.state = loaderFailed
Expand All @@ -262,6 +261,13 @@ func (loader *PluginLoader) Inject() error {
return err
}
}
if pl, ok := pl.(plugin.PluginFx); ok {
err := injectFxPlugin(pl)
if err != nil {
loader.state = loaderFailed
return err
}
}
}

return loader.transition(loaderInjecting, loaderInjected)
Expand Down Expand Up @@ -347,3 +353,8 @@ func injectTracerPlugin(pl plugin.PluginTracer) error {
opentracing.SetGlobalTracer(tracer)
return nil
}

func injectFxPlugin(pl plugin.PluginFx) error {
core.RegisterFXOptionFunc(pl.Options)
return nil
}

0 comments on commit 3a0e18c

Please sign in to comment.