Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/bidirectional/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/counter
/counter-*
/kv_*
4 changes: 2 additions & 2 deletions examples/bidirectional/shared/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (m *GRPCServer) Get(ctx context.Context, req *proto.GetRequest) (*proto.Get
return &proto.GetResponse{Value: v}, err
}

// GRPCClient is an implementation of KV that talks over RPC.
// GRPCAddHelperClient is an implementation of AddHelper that talks over RPC.
type GRPCAddHelperClient struct{ client proto.AddHelperClient }

func (m *GRPCAddHelperClient) Sum(a, b int64) (int64, error) {
Expand All @@ -92,7 +92,7 @@ func (m *GRPCAddHelperClient) Sum(a, b int64) (int64, error) {
return resp.R, err
}

// Here is the gRPC server that GRPCClient talks to.
// Here is the gRPC server that GRPCAddHelperClient talks to.
type GRPCAddHelperServer struct {
// This is the real implementation
Impl AddHelper
Expand Down
2 changes: 1 addition & 1 deletion examples/bidirectional/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type AddHelper interface {
Sum(int64, int64) (int64, error)
}

// KV is the interface that we're exposing as a plugin.
// Counter 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)
Expand Down
16 changes: 10 additions & 6 deletions examples/grpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ $ go build -o kv
# This builds the plugin written in Go
$ go build -o kv-go-grpc ./plugin-go-grpc

# This tells the KV binary to use the "kv-go-grpc" binary
# This tells the KV binary to use the "kv-go-grpc" binary over gRPC
$ export KV_PLUGIN_NAME=kv_grpc
$ export KV_PLUGIN="./kv-go-grpc"

# Read and write
Expand All @@ -24,34 +25,37 @@ world

This plugin uses gRPC to serve a plugin that is written in Go:

```
```sh
# This builds the plugin written in Go
$ go build -o kv-go-grpc ./plugin-go-grpc

# This tells the KV binary to use the "kv-go-grpc" binary
# This tells the KV binary to use the "kv-go-grpc" binary over gRPC
$ export KV_PLUGIN_NAME=kv_grpc
$ export KV_PLUGIN="./kv-go-grpc"
```

### Plugin: plugin-go-netrpc

This plugin uses the builtin Go net/rpc mechanism to serve the plugin:

```
```sh
# This builds the plugin written in Go
$ go build -o kv-go-netrpc ./plugin-go-netrpc

# This tells the KV binary to use the "kv-go-netrpc" binary
# This tells the KV binary to use the "kv-go-netrpc" binary over net/rpc
$ export KV_PLUGIN_NAME=kv
$ export KV_PLUGIN="./kv-go-netrpc"
```

### Plugin: plugin-python

This plugin is written in Python:

```
```sh
$ python -m venv plugin-python/.venv
$ source plugin-python/.venv/bin/activate
$ pip install -r plugin-python/requirements.txt
$ export KV_PLUGIN_NAME=kv_grpc
$ export KV_PLUGIN="python plugin-python/plugin.py"
```

Expand Down
13 changes: 12 additions & 1 deletion examples/grpc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,19 @@ func run() error {
return err
}

var pluginName string
switch os.Getenv("KV_PROTO") {
case "netrpc":
pluginName = shared.PluginNetRPC
case "grpc":
pluginName = shared.PluginGRPC
default:
fmt.Println("must set KV_PROTO to netrpc or grpc")
os.Exit(1)
}

// Request the plugin
raw, err := rpcClient.Dispense("kv_grpc")
raw, err := rpcClient.Dispense(pluginName)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion examples/grpc/plugin-python/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def Get(self, request, context):

def Put(self, request, context):
filename = "kv_"+request.key
value = "{0}\n\nWritten from plugin-python".format(request.value)
value = "{0}\n\nWritten from plugin-python".format(request.value.decode('utf-8'))
with open(filename, 'w') as f:
f.write(value)

Expand Down
9 changes: 7 additions & 2 deletions examples/grpc/shared/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (
"github.com/hashicorp/go-plugin/examples/grpc/proto"
)

const (
PluginNetRPC = "kv"
PluginGRPC = "kv_grpc"
)

// Handshake is a common handshake that is shared by plugin and host.
var Handshake = plugin.HandshakeConfig{
// This isn't required when using VersionedPlugins
Expand All @@ -24,8 +29,8 @@ var Handshake = plugin.HandshakeConfig{

// PluginMap is the map of plugins we can dispense.
var PluginMap = map[string]plugin.Plugin{
"kv_grpc": &KVGRPCPlugin{},
"kv": &KVPlugin{},
PluginGRPC: &KVGRPCPlugin{},
PluginNetRPC: &KVPlugin{},
}

// KV is the interface that we're exposing as a plugin.
Expand Down