Skip to content

Commit

Permalink
Convert errors to serializable errors
Browse files Browse the repository at this point in the history
Plain errors cannot be serialized over gRPC without some gob work so
this change makes use of the internal BasicError type from go-plugin to
send the information back to server.

Upstream bug:

golang/go#23340
  • Loading branch information
sgnn7 committed Sep 3, 2019
1 parent 160c137 commit f8851dc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
6 changes: 3 additions & 3 deletions app/main.go
Expand Up @@ -8,7 +8,7 @@ import (
"os/exec"
"path/filepath"

"github.com/hashicorp/go-plugin"
go_plugin "github.com/hashicorp/go-plugin"

app_plugin "github.com/sgnn7/golang-grpc-plugin-test/app/plugin"
tcp_connector "github.com/sgnn7/golang-grpc-plugin-test/app/plugin/connector/tcp"
Expand All @@ -27,7 +27,7 @@ func (manager *PluginManager) StartPlugin(pluginName string) error {

log.Printf("Starting plugin: %s", pluginFile)

client := plugin.NewClient(&plugin.ClientConfig{
client := go_plugin.NewClient(&go_plugin.ClientConfig{
Cmd: exec.Command(pluginFile),
Managed: true,
SyncStdout: os.Stdout,
Expand Down Expand Up @@ -57,7 +57,7 @@ func (manager *PluginManager) StartPlugin(pluginName string) error {
for {
log.Println()
log.Printf("RCP call to plugin response: %v",
tcpConnectorObj.Connect("tcp://localhost:8080"))
tcpConnectorObj.Connect("localhost:8080"))
time.Sleep(1000 * time.Millisecond)
}
}()
Expand Down
4 changes: 2 additions & 2 deletions app/plugin/connector/tcp/goplugin-client.go
Expand Up @@ -22,10 +22,10 @@ func (tcpConnectorPlugin *TCPConnector) Client(pluginBroker *plugin.MuxBroker,
}, nil
}

func (tcpConnectorClient *tcpConnectorClient) Connect(address string) error {
func (tcpConnectorClient *tcpConnectorClient) Connect(address string) plugin.BasicError {
log.Printf("In: Client Connect: %s", address)

var resp_err error
var resp_err plugin.BasicError

err := tcpConnectorClient.Client.Call("Plugin.Connect", &address, &resp_err)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion app/plugin/connector/tcp/goplugin-server.go
Expand Up @@ -23,7 +23,7 @@ func (tcpConnectorPlugin *TCPConnector) Server(pluginBroker *plugin.MuxBroker) (
}, nil
}

func (tcpConnectorPlugin *tcpConnectorServer) Connect(address string, result *error) error {
func (tcpConnectorPlugin *tcpConnectorServer) Connect(address string, result *plugin.BasicError) error {
log.Printf("In: Server Connect: %s", address)
*result = tcpConnectorPlugin.ITCPConnector.Connect(address)
log.Printf("In: Server Reply Response: %v", *result)
Expand Down
6 changes: 5 additions & 1 deletion app/plugin/connector/tcp/tcp.go
@@ -1,9 +1,13 @@
package tcp

import (
"github.com/hashicorp/go-plugin"
)

const InterfaceName = "TCPConnector"

type ITCPConnector interface {
Connect(address string) error
Connect(address string) plugin.BasicError
}

type TCPConnectorFunc func() ITCPConnector
Expand Down
32 changes: 27 additions & 5 deletions plugin/main.go
Expand Up @@ -2,9 +2,11 @@ package main

import (
"log"
"net"
"time"

"github.com/sgnn7/golang-grpc-plugin-test/app/plugin"
go_plugin "github.com/hashicorp/go-plugin"
app_plugin "github.com/sgnn7/golang-grpc-plugin-test/app/plugin"
tcp_connector "github.com/sgnn7/golang-grpc-plugin-test/app/plugin/connector/tcp"
)

Expand All @@ -18,18 +20,38 @@ func TCPConnector() tcp_connector.ITCPConnector {
return &tcpConnectorImpl{}
}

func (p *tcpConnectorImpl) Connect(address string) error {
func (p *tcpConnectorImpl) Connect(address string) go_plugin.BasicError {
log.Printf("Plugin Connect: %s", address)
return nil

log.Println("Starting plugin client...")
connection, err := net.Dial("tcp", address)
if err != nil {
log.Printf("Plugin Connect Dial Error: %v", err)
return *go_plugin.NewBasicError(err)
}

if err = connection.Close(); err != nil {
log.Printf("Plugin Connect Close Error: %v", err)
return *go_plugin.NewBasicError(err)
}

if _, err = connection.Write([]byte("foobar")); err != nil {
log.Printf("Plugin Connect Write Error: %v", err)
return *go_plugin.NewBasicError(err)
}

log.Printf("Plugin Connect OK!")

return *go_plugin.NewBasicError(nil)
}

func main() {
pluginOpts := &plugin.PluginOpts{
pluginOpts := &app_plugin.PluginOpts{
TCPConnector: TCPConnector,
RunAsPlugin: true,
}

plugin.StartPlugin(pluginOpts, make(chan bool))
app_plugin.StartPlugin(pluginOpts, make(chan bool))

// tcpConnectorPlugin := TCPConnector()
for {
Expand Down

0 comments on commit f8851dc

Please sign in to comment.