open-nomad/helper/pluginutils/loader/instance.go
Mahmood Ali 2588b3bc98 cleanup driver eventor goroutines
This fixes few cases where driver eventor goroutines are leaked during
normal operations, but especially so in tests.

This change makes few modifications:

First, it switches drivers to use `Context`s to manage shutdown events.
Previously, it relied on callers invoking `.Shutdown()` function that is
specific to internal drivers only and require casting.  Using `Contexts`
provide a consistent idiomatic way to manage lifecycle for both internal
and external drivers.

Also, I discovered few places where we don't clean up a temporary driver
instance in the plugin catalog code, where we dispense a driver to
inspect and validate the schema config without properly cleaning it up.
2020-05-26 11:04:04 -04:00

64 lines
2.2 KiB
Go

package loader
import plugin "github.com/hashicorp/go-plugin"
// PluginInstance wraps an instance of a plugin. If the plugin is external, it
// provides methods to retrieve the ReattachConfig and to kill the plugin.
type PluginInstance interface {
// Internal returns if the plugin is internal
Internal() bool
// Kill kills the plugin if it is external. It is safe to call on internal
// plugins.
Kill()
// ReattachConfig returns the ReattachConfig and whether the plugin is internal
// or not. If the second return value is false, no ReattachConfig is
// possible to return.
ReattachConfig() (config *plugin.ReattachConfig, canReattach bool)
// Plugin returns the wrapped plugin instance.
Plugin() interface{}
// Exited returns whether the plugin has exited
Exited() bool
// ApiVersion returns the API version to be used with the plugin
ApiVersion() string
}
// internalPluginInstance wraps an internal plugin
type internalPluginInstance struct {
instance interface{}
apiVersion string
killFn func()
}
func (p *internalPluginInstance) Internal() bool { return true }
func (p *internalPluginInstance) Kill() { p.killFn() }
func (p *internalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) { return nil, false }
func (p *internalPluginInstance) Plugin() interface{} { return p.instance }
func (p *internalPluginInstance) Exited() bool { return false }
func (p *internalPluginInstance) ApiVersion() string { return p.apiVersion }
// externalPluginInstance wraps an external plugin
type externalPluginInstance struct {
client *plugin.Client
instance interface{}
apiVersion string
}
func (p *externalPluginInstance) Internal() bool { return false }
func (p *externalPluginInstance) Plugin() interface{} { return p.instance }
func (p *externalPluginInstance) Exited() bool { return p.client.Exited() }
func (p *externalPluginInstance) ApiVersion() string { return p.apiVersion }
func (p *externalPluginInstance) ReattachConfig() (*plugin.ReattachConfig, bool) {
return p.client.ReattachConfig(), true
}
func (p *externalPluginInstance) Kill() {
p.client.Kill()
}