1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
// Package shared contains shared data between the host and plugins.
package shared
import (
"golang.org/x/net/context"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/go-plugin/examples/bidirectional/proto"
)
// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
"counter": &CounterPlugin{},
}
type AddHelper interface {
Sum(int64, int64) (int64, error)
}
// KV is the interface that we're exposing as a plugin.
type Counter interface {
Put(key string, value int64, a AddHelper) error
Get(key string) (int64, error)
}
// This is the implementation of plugin.Plugin so we can serve/consume this.
// We also implement GRPCPlugin so that this plugin can be served over
// gRPC.
type CounterPlugin struct {
plugin.NetRPCUnsupportedPlugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl Counter
}
func (p *CounterPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
proto.RegisterCounterServer(s, &GRPCServer{
Impl: p.Impl,
broker: broker,
})
return nil
}
func (p *CounterPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{
client: proto.NewCounterClient(c),
broker: broker,
}, nil
}
var _ plugin.GRPCPlugin = &CounterPlugin{}
|