-
Notifications
You must be signed in to change notification settings - Fork 432
/
interface.go
68 lines (55 loc) · 1.88 KB
/
interface.go
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
60
61
62
63
64
65
66
67
68
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// Package shared contains shared data between the host and plugins.
package shared
import (
"context"
"net/rpc"
"google.golang.org/grpc"
"github.com/hashicorp/go-plugin"
"github.com/hashicorp/go-plugin/examples/grpc/proto"
)
// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
ProtocolVersion: 1,
MagicCookieKey: "BASIC_PLUGIN",
MagicCookieValue: "hello",
}
// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
"kv_grpc": &KVGRPCPlugin{},
"kv": &KVPlugin{},
}
// KV is the interface that we're exposing as a plugin.
type KV interface {
Put(key string, value []byte) error
Get(key string) ([]byte, error)
}
// This is the implementation of plugin.Plugin so we can serve/consume this.
type KVPlugin struct {
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl KV
}
func (p *KVPlugin) Server(*plugin.MuxBroker) (interface{}, error) {
return &RPCServer{Impl: p.Impl}, nil
}
func (*KVPlugin) Client(b *plugin.MuxBroker, c *rpc.Client) (interface{}, error) {
return &RPCClient{client: c}, nil
}
// This is the implementation of plugin.GRPCPlugin so we can serve/consume this.
type KVGRPCPlugin struct {
// GRPCPlugin must still implement the Plugin interface
plugin.Plugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl KV
}
func (p *KVGRPCPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
proto.RegisterKVServer(s, &GRPCServer{Impl: p.Impl})
return nil
}
func (p *KVGRPCPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{client: proto.NewKVClient(c)}, nil
}