-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
9 changed files
with
119 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package fxtest | ||
|
||
import ( | ||
"os" | ||
|
||
logging "github.com/ipfs/go-log" | ||
"github.com/ipfs/kubo/plugin" | ||
"go.uber.org/fx" | ||
) | ||
|
||
var log = logging.Logger("fxtestplugin") | ||
|
||
var Plugins = []plugin.Plugin{ | ||
&fxtestPlugin{}, | ||
} | ||
|
||
// fxtestPlugin is used for testing the fx plugin. | ||
// It merely adds an fx option that logs a debug statement, so we can verify that it works in tests. | ||
type fxtestPlugin struct{} | ||
|
||
var _ plugin.PluginFx = (*fxtestPlugin)(nil) | ||
|
||
func (p *fxtestPlugin) Name() string { | ||
return "fx-test" | ||
} | ||
|
||
func (p *fxtestPlugin) Version() string { | ||
return "0.1.0" | ||
} | ||
|
||
func (p *fxtestPlugin) Init(env *plugin.Environment) error { | ||
return nil | ||
} | ||
|
||
func (p *fxtestPlugin) Options(opts []fx.Option) ([]fx.Option, error) { | ||
if os.Getenv("TEST_FX_PLUGIN") != "" { | ||
opts = append(opts, fx.Invoke(func() { | ||
log.Debug("invoked test fx function") | ||
})) | ||
} | ||
return opts, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env bash | ||
|
||
test_description="Test fx plugin" | ||
|
||
. lib/test-lib.sh | ||
|
||
test_init_ipfs | ||
|
||
export GOLOG_LOG_LEVEL="fxtestplugin=debug" | ||
export TEST_FX_PLUGIN=1 | ||
test_launch_ipfs_daemon | ||
|
||
test_expect_success "expected log entry should be present" ' | ||
fgrep "invoked test fx function" daemon_err >/dev/null | ||
' | ||
|
||
test_kill_ipfs_daemon | ||
|
||
test_done |