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

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