Skip to content

Commit

Permalink
Constrain GRPC load balancing to services already using it
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Cobden committed Sep 19, 2018
1 parent f15aebe commit dfba8f2
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion common/billing/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {
// NewClient creates... a new client.
func NewClient(cfg Config) (*Client, error) {
log.WithField("url", cfg.HostPort).Infof("creating gRPC client")
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, "", durationCollector)
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, false, "", durationCollector)
if err != nil {
return nil, err
}
Expand Down
23 changes: 16 additions & 7 deletions common/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,28 @@ func instrumetation(errorKey string, durationCollector *instrument.HistogramColl
}
}

func dial(url string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
address, dialOptions, err := server.ParseURL(url)
if err != nil {
return nil, err
func dial(urlOrHostPort string, loadBalance bool, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
var dialOptions []grpc.DialOption
var address string
// Passing this flag is a bit ugly, but we might re-do the load balancing soon
if loadBalance {
var err error
address, dialOptions, err = server.ParseURL(urlOrHostPort)
if err != nil {
return nil, err
}
dialOptions = append(dialOptions, opts...)
} else {
dialOptions = opts
address = urlOrHostPort
}
dialOptions = append(dialOptions, opts...)
return grpc.Dial(address, dialOptions...)
}

// NewInsecureConn instantiates ClientConn with middleware.
func NewInsecureConn(url string, errorKey string, durationCollector *instrument.HistogramCollector) (*grpc.ClientConn, error) {
func NewInsecureConn(urlOrHostPort string, loadBalance bool, errorKey string, durationCollector *instrument.HistogramCollector) (*grpc.ClientConn, error) {
opts := append(
[]grpc.DialOption{grpc.WithInsecure()},
instrumetation(errorKey, durationCollector)...)
return dial(url, opts...)
return dial(urlOrHostPort, loadBalance, opts...)
}
2 changes: 1 addition & 1 deletion common/users/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {

// NewClient instantiates Client.
func NewClient(cfg Config) (*Client, error) {
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, UsersErrorCode, durationCollector)
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, true, UsersErrorCode, durationCollector)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion users-sync/api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Client struct {

// NewClient instantiates Client.
func NewClient(cfg Config) (CloseableUsersSyncClient, error) {
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, "", durationCollector)
conn, err := common_grpc.NewInsecureConn(cfg.HostPort, false, "", durationCollector)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion users/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func New(kind, address string, opts CachingClientConfig) (users.UsersClient, err
}

func newGRPCClient(address string) (users.UsersClient, error) {
conn, err := common_grpc.NewInsecureConn(address, "", nil)
conn, err := common_grpc.NewInsecureConn(address, true, "", nil)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit dfba8f2

Please sign in to comment.