Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gapic: support max req & res size limits #165

Merged
merged 6 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions internal/gengapic/client_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,22 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN
p(" option.WithEndpoint(%q),", host)
p(" option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),")
p(" option.WithScopes(DefaultAuthScopes()...),")
p(" option.WithGRPCDialOption(grpc.WithDefaultCallOptions(")
p(" grpc.MaxCallRecvMsgSize(math.MaxInt32))),")
p(" }")
p("}")
p("")

g.imports[pbinfo.ImportSpec{Path: "math"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/option"}] = true
}

// defaultCallOptions
{
sFQN := fmt.Sprintf("%s.%s", g.descInfo.ParentFile[serv].GetPackage(), serv.GetName())
policies := map[string]*conf.MethodConfig_RetryPolicy{}
reqLimits := map[string]int{}
resLimits := map[string]int{}

var methCfgs []*conf.MethodConfig
if g.grpcConf != nil {
Expand All @@ -92,16 +97,41 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN
if name.GetMethod() != "" {
base = base + "." + name.GetMethod()
policies[base] = mc.GetRetryPolicy()

if maxReq := mc.GetMaxRequestMessageBytes(); maxReq != nil {
reqLimits[base] = int(maxReq.GetValue())
}

if maxRes := mc.GetMaxResponseMessageBytes(); maxRes != nil {
resLimits[base] = int(maxRes.GetValue())
}

continue
}

// service-level config, apply to all *unset* methods
for _, m := range serv.GetMethod() {
// build fully-qualified name
fqn := base + "." + m.GetName()

// set retry config
if _, ok := policies[fqn]; !ok {
policies[fqn] = mc.GetRetryPolicy()
}

// set max request size limit
if maxReq := mc.GetMaxRequestMessageBytes(); maxReq != nil {
if _, ok := reqLimits[fqn]; !ok {
reqLimits[fqn] = int(maxReq.GetValue())
}
}

// set max response size limit
if maxRes := mc.GetMaxResponseMessageBytes(); maxRes != nil {
if _, ok := resLimits[fqn]; !ok {
resLimits[fqn] = int(maxRes.GetValue())
}
}
}
}
}
Expand All @@ -117,6 +147,15 @@ func (g *generator) clientOptions(serv *descriptor.ServiceDescriptorProto, servN
for _, m := range serv.GetMethod() {
mFQN := sFQN + "." + m.GetName()
p("%s: []gax.CallOption{", m.GetName())

if maxReq, ok := reqLimits[mFQN]; ok {
p("gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(%d)),", maxReq)
}

if maxRes, ok := resLimits[mFQN]; ok {
p("gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(%d)),", maxRes)
}

if rp, ok := policies[mFQN]; ok && rp != nil {
p("gax.WithRetry(func() gax.Retryer {")
p(" return gax.OnCodes([]codes.Code{")
Expand Down
5 changes: 5 additions & 0 deletions internal/gengapic/client_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/googleapis/gapic-generator-go/internal/txtdiff"
"google.golang.org/genproto/googleapis/api/annotations"
code "google.golang.org/genproto/googleapis/rpc/code"
wrappers "github.com/golang/protobuf/ptypes/wrappers"
)

func TestClientOpt(t *testing.T) {
Expand All @@ -40,6 +41,8 @@ func TestClientOpt(t *testing.T) {
Method: "Zip",
},
},
MaxRequestMessageBytes: &wrappers.UInt32Value{Value: 123456},
MaxResponseMessageBytes: &wrappers.UInt32Value{Value: 123456},
RetryOrHedgingPolicy: &conf.MethodConfig_RetryPolicy_{
RetryPolicy: &conf.MethodConfig_RetryPolicy{
InitialBackoff: &duration.Duration{Nanos: 100000000},
Expand All @@ -57,6 +60,8 @@ func TestClientOpt(t *testing.T) {
Service: "bar.FooService",
},
},
MaxRequestMessageBytes: &wrappers.UInt32Value{Value: 654321},
MaxResponseMessageBytes: &wrappers.UInt32Value{Value: 654321},
RetryOrHedgingPolicy: &conf.MethodConfig_RetryPolicy_{
RetryPolicy: &conf.MethodConfig_RetryPolicy{
InitialBackoff: &duration.Duration{Nanos: 10000000},
Expand Down
1 change: 0 additions & 1 deletion internal/gengapic/paging.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ func (g *generator) pagingCall(servName string, m *descriptor.MethodDescriptorPr
p("}")
p("")

g.imports[pbinfo.ImportSpec{Path: "math"}] = true
g.imports[pbinfo.ImportSpec{Path: "github.com/golang/protobuf/proto"}] = true
g.imports[pbinfo.ImportSpec{Path: "google.golang.org/api/iterator"}] = true
g.imports[inSpec] = true
Expand Down
8 changes: 8 additions & 0 deletions internal/gengapic/testdata/empty_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ func defaultClientOptions() []option.ClientOption {
option.WithEndpoint("foo.bar.com:443"),
option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
option.WithScopes(DefaultAuthScopes()...),
option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

func defaultCallOptions() *CallOptions {
return &CallOptions{
Zip: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(123456)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(123456)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand All @@ -27,6 +31,8 @@ func defaultCallOptions() *CallOptions {
}),
},
Zap: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(654321)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(654321)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand All @@ -38,6 +44,8 @@ func defaultCallOptions() *CallOptions {
}),
},
Smack: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(654321)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(654321)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand Down
8 changes: 8 additions & 0 deletions internal/gengapic/testdata/foo_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ func defaultFooClientOptions() []option.ClientOption {
option.WithEndpoint("foo.bar.com:443"),
option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
option.WithScopes(DefaultAuthScopes()...),
option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

func defaultFooCallOptions() *FooCallOptions {
return &FooCallOptions{
Zip: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(123456)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(123456)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand All @@ -27,6 +31,8 @@ func defaultFooCallOptions() *FooCallOptions {
}),
},
Zap: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(654321)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(654321)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand All @@ -38,6 +44,8 @@ func defaultFooCallOptions() *FooCallOptions {
}),
},
Smack: []gax.CallOption{
gax.WithGRPCOptions(grpc.MaxCallSendMsgSize(654321)),
gax.WithGRPCOptions(grpc.MaxCallRecvMsgSize(654321)),
gax.WithRetry(func() gax.Retryer {
return gax.OnCodes([]codes.Code{
codes.Unknown,
Expand Down
2 changes: 2 additions & 0 deletions internal/gengapic/testdata/host_port_opt.want
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ func defaultBarClientOptions() []option.ClientOption {
option.WithEndpoint("foo.bar.com:1234"),
option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
option.WithScopes(DefaultAuthScopes()...),
option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
grpc.MaxCallRecvMsgSize(math.MaxInt32))),
}
}

Expand Down