github.com/hashicorp/go-plugin@v1.6.0/examples/grpc/shared/rpc.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package shared
     5  
     6  import (
     7  	"net/rpc"
     8  )
     9  
    10  // RPCClient is an implementation of KV that talks over RPC.
    11  type RPCClient struct{ client *rpc.Client }
    12  
    13  func (m *RPCClient) Put(key string, value []byte) error {
    14  	// We don't expect a response, so we can just use interface{}
    15  	var resp interface{}
    16  
    17  	// The args are just going to be a map. A struct could be better.
    18  	return m.client.Call("Plugin.Put", map[string]interface{}{
    19  		"key":   key,
    20  		"value": value,
    21  	}, &resp)
    22  }
    23  
    24  func (m *RPCClient) Get(key string) ([]byte, error) {
    25  	var resp []byte
    26  	err := m.client.Call("Plugin.Get", key, &resp)
    27  	return resp, err
    28  }
    29  
    30  // Here is the RPC server that RPCClient talks to, conforming to
    31  // the requirements of net/rpc
    32  type RPCServer struct {
    33  	// This is the real implementation
    34  	Impl KV
    35  }
    36  
    37  func (m *RPCServer) Put(args map[string]interface{}, resp *interface{}) error {
    38  	return m.Impl.Put(args["key"].(string), args["value"].([]byte))
    39  }
    40  
    41  func (m *RPCServer) Get(key string, resp *[]byte) error {
    42  	v, err := m.Impl.Get(key)
    43  	*resp = v
    44  	return err
    45  }