plugin.go 2.0 KB

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