Skip to content

Commit

Permalink
move protobuf files into internal repo
Browse files Browse the repository at this point in the history
Remove the protobuf files from the plugin package, to avoid polluting
the public API.
  • Loading branch information
jbardin committed Dec 11, 2018
1 parent 26219a0 commit 1b0f542
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 57 deletions.
40 changes: 20 additions & 20 deletions grpc_broker.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:generate protoc -I ./ ./grpc_broker.proto --go_out=plugins=grpc:.

package plugin

import (
Expand All @@ -13,6 +11,8 @@ import (
"sync/atomic"
"time"

"github.com/hashicorp/go-plugin/internal/proto"

"github.com/oklog/run"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand All @@ -21,14 +21,14 @@ import (
// streamer interface is used in the broker to send/receive connection
// information.
type streamer interface {
Send(*ConnInfo) error
Recv() (*ConnInfo, error)
Send(*proto.ConnInfo) error
Recv() (*proto.ConnInfo, error)
Close()
}

// sendErr is used to pass errors back during a send.
type sendErr struct {
i *ConnInfo
i *proto.ConnInfo
ch chan error
}

Expand All @@ -40,7 +40,7 @@ type gRPCBrokerServer struct {
send chan *sendErr

// recv is used to receive connection info from the gRPC stream.
recv chan *ConnInfo
recv chan *proto.ConnInfo

// quit closes down the stream.
quit chan struct{}
Expand All @@ -52,15 +52,15 @@ type gRPCBrokerServer struct {
func newGRPCBrokerServer() *gRPCBrokerServer {
return &gRPCBrokerServer{
send: make(chan *sendErr),
recv: make(chan *ConnInfo),
recv: make(chan *proto.ConnInfo),
quit: make(chan struct{}),
}
}

// StartStream implements the GRPCBrokerServer interface and will block until
// the quit channel is closed or the context reports Done. The stream will pass
// connection information to/from the client.
func (s *gRPCBrokerServer) StartStream(stream GRPCBroker_StartStreamServer) error {
func (s *gRPCBrokerServer) StartStream(stream proto.GRPCBroker_StartStreamServer) error {
doneCh := stream.Context().Done()
defer s.Close()

Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *gRPCBrokerServer) StartStream(stream GRPCBroker_StartStreamServer) erro

// Send is used by the GRPCBroker to pass connection information into the stream
// to the client.
func (s *gRPCBrokerServer) Send(i *ConnInfo) error {
func (s *gRPCBrokerServer) Send(i *proto.ConnInfo) error {
ch := make(chan error)
defer close(ch)

Expand All @@ -117,7 +117,7 @@ func (s *gRPCBrokerServer) Send(i *ConnInfo) error {

// Recv is used by the GRPCBroker to pass connection information that has been
// sent from the client from the stream to the broker.
func (s *gRPCBrokerServer) Recv() (*ConnInfo, error) {
func (s *gRPCBrokerServer) Recv() (*proto.ConnInfo, error) {
select {
case <-s.quit:
return nil, errors.New("broker closed")
Expand All @@ -138,13 +138,13 @@ func (s *gRPCBrokerServer) Close() {
// streamer interfaces.
type gRPCBrokerClientImpl struct {
// client is the underlying GRPC client used to make calls to the server.
client GRPCBrokerClient
client proto.GRPCBrokerClient

// send is used to send connection info to the gRPC stream.
send chan *sendErr

// recv is used to receive connection info from the gRPC stream.
recv chan *ConnInfo
recv chan *proto.ConnInfo

// quit closes down the stream.
quit chan struct{}
Expand All @@ -155,9 +155,9 @@ type gRPCBrokerClientImpl struct {

func newGRPCBrokerClient(conn *grpc.ClientConn) *gRPCBrokerClientImpl {
return &gRPCBrokerClientImpl{
client: NewGRPCBrokerClient(conn),
client: proto.NewGRPCBrokerClient(conn),
send: make(chan *sendErr),
recv: make(chan *ConnInfo),
recv: make(chan *proto.ConnInfo),
quit: make(chan struct{}),
}
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *gRPCBrokerClientImpl) StartStream() error {

// Send is used by the GRPCBroker to pass connection information into the stream
// to the plugin.
func (s *gRPCBrokerClientImpl) Send(i *ConnInfo) error {
func (s *gRPCBrokerClientImpl) Send(i *proto.ConnInfo) error {
ch := make(chan error)
defer close(ch)

Expand All @@ -227,7 +227,7 @@ func (s *gRPCBrokerClientImpl) Send(i *ConnInfo) error {

// Recv is used by the GRPCBroker to pass connection information that has been
// sent from the plugin to the broker.
func (s *gRPCBrokerClientImpl) Recv() (*ConnInfo, error) {
func (s *gRPCBrokerClientImpl) Recv() (*proto.ConnInfo, error) {
select {
case <-s.quit:
return nil, errors.New("broker closed")
Expand Down Expand Up @@ -268,7 +268,7 @@ type GRPCBroker struct {
}

type gRPCBrokerPending struct {
ch chan *ConnInfo
ch chan *proto.ConnInfo
doneCh chan struct{}
}

Expand All @@ -290,7 +290,7 @@ func (b *GRPCBroker) Accept(id uint32) (net.Listener, error) {
return nil, err
}

err = b.streamer.Send(&ConnInfo{
err = b.streamer.Send(&proto.ConnInfo{
ServiceId: id,
Network: listener.Addr().Network(),
Address: listener.Addr().String(),
Expand Down Expand Up @@ -365,7 +365,7 @@ func (b *GRPCBroker) Close() error {

// Dial opens a connection by ID.
func (b *GRPCBroker) Dial(id uint32) (conn *grpc.ClientConn, err error) {
var c *ConnInfo
var c *proto.ConnInfo

// Open the stream
p := b.getStream(id)
Expand Down Expand Up @@ -435,7 +435,7 @@ func (m *GRPCBroker) getStream(id uint32) *gRPCBrokerPending {
}

m.streams[id] = &gRPCBrokerPending{
ch: make(chan *ConnInfo, 1),
ch: make(chan *proto.ConnInfo, 1),
doneCh: make(chan struct{}),
}
return m.streams[id]
Expand Down
7 changes: 4 additions & 3 deletions grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"time"

"github.com/hashicorp/go-plugin/internal/proto"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -60,7 +61,7 @@ func newGRPCClient(doneCtx context.Context, c *Client) (*GRPCClient, error) {
Plugins: c.config.Plugins,
doneCtx: doneCtx,
broker: broker,
controller: NewGRPCControllerClient(conn),
controller: proto.NewGRPCControllerClient(conn),
}

return cl, nil
Expand All @@ -74,13 +75,13 @@ type GRPCClient struct {
doneCtx context.Context
broker *GRPCBroker

controller GRPCControllerClient
controller proto.GRPCControllerClient
}

// ClientProtocol impl.
func (c *GRPCClient) Close() error {
c.broker.Close()
c.controller.Shutdown(c.doneCtx, &Empty{})
c.controller.Shutdown(c.doneCtx, &proto.Empty{})
return c.Conn.Close()
}

Expand Down
7 changes: 4 additions & 3 deletions grpc_controller.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
//go:generate protoc -I ./ ./grpc_controller.proto --go_out=plugins=grpc:.
package plugin

import (
"context"

"github.com/hashicorp/go-plugin/internal/proto"
)

// GRPCControllerServer handles shutdown calls to terminate the server when the
Expand All @@ -13,8 +14,8 @@ type grpcControllerServer struct {

// Shutdown stops the grpc server. It first will attempt a graceful stop, then a
// full stop on the server.
func (s *grpcControllerServer) Shutdown(ctx context.Context, _ *Empty) (*Empty, error) {
resp := &Empty{}
func (s *grpcControllerServer) Shutdown(ctx context.Context, _ *proto.Empty) (*proto.Empty, error) {
resp := &proto.Empty{}

// TODO: figure out why GracefullStop doesn't work.
s.server.Stop()
Expand Down
5 changes: 3 additions & 2 deletions grpc_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log"
"net"

"github.com/hashicorp/go-plugin/internal/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/health"
Expand Down Expand Up @@ -72,15 +73,15 @@ func (s *GRPCServer) Init() error {

// Register the broker service
brokerServer := newGRPCBrokerServer()
RegisterGRPCBrokerServer(s.server, brokerServer)
proto.RegisterGRPCBrokerServer(s.server, brokerServer)
s.broker = newGRPCBroker(brokerServer, s.TLS)
go s.broker.Run()

// Register the controller
controllerServer := &grpcControllerServer{
server: s,
}
RegisterGRPCControllerServer(s.server, controllerServer)
proto.RegisterGRPCControllerServer(s.server, controllerServer)

// Register all our plugins onto the gRPC server.
for k, raw := range s.Plugins {
Expand Down
3 changes: 3 additions & 0 deletions internal/proto/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//go:generate protoc -I ./ ./grpc_broker.proto ./grpc_controller.proto --go_out=plugins=grpc:.

package proto
30 changes: 15 additions & 15 deletions grpc_broker.pb.go → internal/proto/grpc_broker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion grpc_broker.proto → internal/proto/grpc_broker.proto
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package plugin;
package proto;

message ConnInfo {
uint32 service_id = 1;
Expand Down
22 changes: 11 additions & 11 deletions grpc_controller.pb.go → internal/proto/grpc_controller.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
syntax = "proto3";
package plugin;
package proto;

message Empty {
}
Expand Down
Loading

0 comments on commit 1b0f542

Please sign in to comment.