OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / hashicorp / go-plugin / plugin.go
1 // The plugin package exposes functions and helpers for communicating to
2 // plugins which are implemented as standalone binary applications.
3 //
4 // plugin.Client fully manages the lifecycle of executing the application,
5 // connecting to it, and returning the RPC client for dispensing plugins.
6 //
7 // plugin.Serve fully manages listeners to expose an RPC server from a binary
8 // that plugin.Client can connect to.
9 package plugin
10
11 import (
12         "context"
13         "errors"
14         "net/rpc"
15
16         "google.golang.org/grpc"
17 )
18
19 // Plugin is the interface that is implemented to serve/connect to an
20 // inteface implementation.
21 type Plugin interface {
22         // Server should return the RPC server compatible struct to serve
23         // the methods that the Client calls over net/rpc.
24         Server(*MuxBroker) (interface{}, error)
25
26         // Client returns an interface implementation for the plugin you're
27         // serving that communicates to the server end of the plugin.
28         Client(*MuxBroker, *rpc.Client) (interface{}, error)
29 }
30
31 // GRPCPlugin is the interface that is implemented to serve/connect to
32 // a plugin over gRPC.
33 type GRPCPlugin interface {
34         // GRPCServer should register this plugin for serving with the
35         // given GRPCServer. Unlike Plugin.Server, this is only called once
36         // since gRPC plugins serve singletons.
37         GRPCServer(*GRPCBroker, *grpc.Server) error
38
39         // GRPCClient should return the interface implementation for the plugin
40         // you're serving via gRPC. The provided context will be canceled by
41         // go-plugin in the event of the plugin process exiting.
42         GRPCClient(context.Context, *GRPCBroker, *grpc.ClientConn) (interface{}, error)
43 }
44
45 // NetRPCUnsupportedPlugin implements Plugin but returns errors for the
46 // Server and Client functions. This will effectively disable support for
47 // net/rpc based plugins.
48 //
49 // This struct can be embedded in your struct.
50 type NetRPCUnsupportedPlugin struct{}
51
52 func (p NetRPCUnsupportedPlugin) Server(*MuxBroker) (interface{}, error) {
53         return nil, errors.New("net/rpc plugin protocol not supported")
54 }
55
56 func (p NetRPCUnsupportedPlugin) Client(*MuxBroker, *rpc.Client) (interface{}, error) {
57         return nil, errors.New("net/rpc plugin protocol not supported")
58 }