-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
plugin: add a daemon plugin with access to the CoreAPI #5955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
01514d5
a9f2aee
6b676d2
45734eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package plugin | ||
|
||
import ( | ||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
) | ||
|
||
// PluginDaemon is an interface for daemon plugins. These plugins will be run on | ||
// the daemon and will be given access to an implementation of the CoreAPI. | ||
type PluginDaemon interface { | ||
Plugin | ||
|
||
Start(coreiface.CoreAPI) error | ||
Close() error | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,13 @@ package loader | |
|
||
import ( | ||
"fmt" | ||
"github.com/ipfs/go-ipfs/core/coredag" | ||
"github.com/ipfs/go-ipfs/plugin" | ||
"github.com/ipfs/go-ipfs/repo/fsrepo" | ||
"os" | ||
"strings" | ||
|
||
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface" | ||
coredag "github.com/ipfs/go-ipfs/core/coredag" | ||
plugin "github.com/ipfs/go-ipfs/plugin" | ||
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo" | ||
|
||
ipld "gx/ipfs/QmRL22E4paat7ky7vx9MLpR97JHHbFPrg3ytFQw6qp1y1s/go-ipld-format" | ||
opentracing "gx/ipfs/QmWLWmRVSiagqP15jczsGME1qpob6HDbtbHAY2he9W5iUo/opentracing-go" | ||
|
@@ -69,7 +72,7 @@ func loadDynamicPlugins(pluginDir string) ([]plugin.Plugin, error) { | |
return loadPluginsFunc(pluginDir) | ||
} | ||
|
||
//Initialize all loaded plugins | ||
// Initialize initializes all loaded plugins | ||
func (loader *PluginLoader) Initialize() error { | ||
for _, p := range loader.plugins { | ||
err := p.Init() | ||
|
@@ -81,41 +84,83 @@ func (loader *PluginLoader) Initialize() error { | |
return nil | ||
} | ||
|
||
//Run the plugins | ||
func (loader *PluginLoader) Run() error { | ||
// Inject hooks all the plugins into the appropriate subsystems. | ||
func (loader *PluginLoader) Inject() error { | ||
for _, pl := range loader.plugins { | ||
switch pl := pl.(type) { | ||
case plugin.PluginIPLD: | ||
err := runIPLDPlugin(pl) | ||
if pl, ok := pl.(plugin.PluginIPLD); ok { | ||
magik6k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
err := injectIPLDPlugin(pl) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
if pl, ok := pl.(plugin.PluginTracer); ok { | ||
err := injectTracerPlugin(pl) | ||
if err != nil { | ||
return err | ||
} | ||
case plugin.PluginTracer: | ||
err := runTracerPlugin(pl) | ||
} | ||
if pl, ok := pl.(plugin.PluginDatastore); ok { | ||
err := injectDatastorePlugin(pl) | ||
if err != nil { | ||
return err | ||
} | ||
case plugin.PluginDatastore: | ||
err := fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser()) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// Start starts all long-running plugins. | ||
func (loader *PluginLoader) Start(iface coreiface.CoreAPI) error { | ||
for i, pl := range loader.plugins { | ||
if pl, ok := pl.(plugin.PluginDaemon); ok { | ||
err := pl.Start(iface) | ||
if err != nil { | ||
closePlugins(loader.plugins[i:]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is a bug. It should read There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems to be the case, do you mind sending a PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll send a PR. |
||
return err | ||
} | ||
default: | ||
panic(pl) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func runIPLDPlugin(pl plugin.PluginIPLD) error { | ||
// StopDaemon stops all long-running plugins. | ||
func (loader *PluginLoader) Close() error { | ||
return closePlugins(loader.plugins) | ||
} | ||
|
||
func closePlugins(plugins []plugin.Plugin) error { | ||
var errs []string | ||
for _, pl := range plugins { | ||
if pl, ok := pl.(plugin.PluginDaemon); ok { | ||
err := pl.Close() | ||
if err != nil { | ||
errs = append(errs, fmt.Sprintf( | ||
"error closing plugin %s: %s", | ||
pl.Name(), | ||
err.Error(), | ||
)) | ||
} | ||
} | ||
} | ||
if errs != nil { | ||
return fmt.Errorf(strings.Join(errs, "\n")) | ||
} | ||
return nil | ||
} | ||
|
||
func injectDatastorePlugin(pl plugin.PluginDatastore) error { | ||
return fsrepo.AddDatastoreConfigHandler(pl.DatastoreTypeName(), pl.DatastoreConfigParser()) | ||
} | ||
|
||
func injectIPLDPlugin(pl plugin.PluginIPLD) error { | ||
err := pl.RegisterBlockDecoders(ipld.DefaultBlockDecoder) | ||
if err != nil { | ||
return err | ||
} | ||
return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers) | ||
} | ||
|
||
func runTracerPlugin(pl plugin.PluginTracer) error { | ||
func injectTracerPlugin(pl plugin.PluginTracer) error { | ||
tracer, err := pl.InitTracer() | ||
if err != nil { | ||
return err | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Afaik there is a repo with this plugin somewhere, would be nice to have a link
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Linked.