plugin.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. import (
  11. "context"
  12. "errors"
  13. "net/rpc"
  14. "google.golang.org/grpc"
  15. )
  16. // Plugin is the interface that is implemented to serve/connect to an
  17. // inteface implementation.
  18. type Plugin interface {
  19. // Server should return the RPC server compatible struct to serve
  20. // the methods that the Client calls over net/rpc.
  21. Server(*MuxBroker) (interface{}, error)
  22. // Client returns an interface implementation for the plugin you're
  23. // serving that communicates to the server end of the plugin.
  24. Client(*MuxBroker, *rpc.Client) (interface{}, error)
  25. }
  26. // GRPCPlugin is the interface that is implemented to serve/connect to
  27. // a plugin over gRPC.
  28. type GRPCPlugin interface {
  29. // GRPCServer should register this plugin for serving with the
  30. // given GRPCServer. Unlike Plugin.Server, this is only called once
  31. // since gRPC plugins serve singletons.
  32. GRPCServer(*GRPCBroker, *grpc.Server) error
  33. // GRPCClient should return the interface implementation for the plugin
  34. // you're serving via gRPC. The provided context will be canceled by
  35. // go-plugin in the event of the plugin process exiting.
  36. GRPCClient(context.Context, *GRPCBroker, *grpc.ClientConn) (interface{}, error)
  37. }
  38. // NetRPCUnsupportedPlugin implements Plugin but returns errors for the
  39. // Server and Client functions. This will effectively disable support for
  40. // net/rpc based plugins.
  41. //
  42. // This struct can be embedded in your struct.
  43. type NetRPCUnsupportedPlugin struct{}
  44. func (p NetRPCUnsupportedPlugin) Server(*MuxBroker) (interface{}, error) {
  45. return nil, errors.New("net/rpc plugin protocol not supported")
  46. }
  47. func (p NetRPCUnsupportedPlugin) Client(*MuxBroker, *rpc.Client) (interface{}, error) {
  48. return nil, errors.New("net/rpc plugin protocol not supported")
  49. }