Skip to content

Commit

Permalink
Merge pull request #168 from lujiajing1126/feature/allow-grpc-dial-op…
Browse files Browse the repository at this point in the history
…tion

Add DialOptions field to Config struct
  • Loading branch information
mitchellh committed Jun 3, 2021
2 parents 044aadd + ac2c48e commit 87f3bd0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
7 changes: 6 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import (
"sync/atomic"
"time"

hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-hclog"
"google.golang.org/grpc"
)

// If this is 1, then we've called CleanupClients. This can be used
Expand Down Expand Up @@ -203,6 +204,10 @@ type ClientConfig struct {
//
// You cannot Reattach to a server with this option enabled.
AutoMTLS bool

// DialOptions allows plugin users to pass custom grpc.DialOption
// to create connections
DialOptions []grpc.DialOption
}

// ReattachConfig is used to configure a client to reattach to an
Expand Down
8 changes: 5 additions & 3 deletions grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"google.golang.org/grpc/health/grpc_health_v1"
)

func dialGRPCConn(tls *tls.Config, dialer func(string, time.Duration) (net.Conn, error)) (*grpc.ClientConn, error) {
func dialGRPCConn(tls *tls.Config, dialer func(string, time.Duration) (net.Conn, error), dialOpts ...grpc.DialOption) (*grpc.ClientConn, error) {
// Build dialing options.
opts := make([]grpc.DialOption, 0, 5)
opts := make([]grpc.DialOption, 0)

// We use a custom dialer so that we can connect over unix domain sockets.
opts = append(opts, grpc.WithDialer(dialer))
Expand All @@ -37,6 +37,8 @@ func dialGRPCConn(tls *tls.Config, dialer func(string, time.Duration) (net.Conn,
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(math.MaxInt32)),
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(math.MaxInt32)))

opts = append(opts, dialOpts...)

// Connect. Note the first parameter is unused because we use a custom
// dialer that has the state to see the address.
conn, err := grpc.Dial("unused", opts...)
Expand All @@ -50,7 +52,7 @@ func dialGRPCConn(tls *tls.Config, dialer func(string, time.Duration) (net.Conn,
// newGRPCClient creates a new GRPCClient. The Client argument is expected
// to be successfully started already with a lock held.
func newGRPCClient(doneCtx context.Context, c *Client) (*GRPCClient, error) {
conn, err := dialGRPCConn(c.config.TLSConfig, c.dialer)
conn, err := dialGRPCConn(c.config.TLSConfig, c.dialer, c.config.DialOptions...)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 87f3bd0

Please sign in to comment.