Add plugin interface code in protobufs package (#921)

Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
This commit is contained in:
Shivam Sandbhor 2021-08-31 18:10:17 +05:30 committed by GitHub
parent 68c11dd827
commit b40fd36607
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,47 @@
package protobufs
import (
"context"
plugin "github.com/hashicorp/go-plugin"
"google.golang.org/grpc"
)
type Notifier interface {
Notify(ctx context.Context, notification *Notification) (*Empty, error)
Configure(ctx context.Context, config *Config) (*Empty, error)
}
// This is the implementation of plugin.NotifierPlugin so we can serve/consume this.
type NotifierPlugin struct {
// GRPCPlugin must still implement the Plugin interface
plugin.Plugin
// Concrete implementation, written in Go. This is only used for plugins
// that are written in Go.
Impl Notifier
}
type GRPCClient struct{ client NotifierClient }
func (m *GRPCClient) Notify(ctx context.Context, notification *Notification) (*Empty, error) {
_, err := m.client.Notify(context.Background(), notification)
return &Empty{}, err
}
func (m *GRPCClient) Configure(ctx context.Context, config *Config) (*Empty, error) {
_, err := m.client.Configure(context.Background(), config)
return &Empty{}, err
}
type GRPCServer struct {
Impl Notifier
}
func (p *NotifierPlugin) GRPCServer(broker *plugin.GRPCBroker, s *grpc.Server) error {
RegisterNotifierServer(s, p.Impl)
return nil
}
func (p *NotifierPlugin) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c *grpc.ClientConn) (interface{}, error) {
return &GRPCClient{client: NewNotifierClient(c)}, nil
}