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

Increase gRPC maximum message size send and receive limits to 256MB #139

Merged
merged 2 commits into from
Jan 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/139.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
tfprotov5/tf5server: Increased maximum gRPC send and receive message size limit to 256MB
```

```release-note:enhancement
tfprotov6/tf6server: Increased maximum gRPC send and receive message size limit to 256MB
```
28 changes: 27 additions & 1 deletion tfprotov5/tf5server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/fromproto"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/tfplugin5"
"github.com/hashicorp/terraform-plugin-go/tfprotov5/internal/toproto"
"google.golang.org/grpc"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
Expand Down Expand Up @@ -59,6 +60,26 @@ const (
envTfReattachProviders = "TF_REATTACH_PROVIDERS"
)

const (
// grpcMaxMessageSize is the maximum gRPC send and receive message sizes
// for the server.
//
// This 256MB value is arbitrarily raised from the default message sizes of
// 4MB to account for advanced use cases, but arbitrarily lowered from
// MaxInt32 (or similar) to prevent incorrect server implementations from
// exhausting resources in common execution environments. Receiving a gRPC
// message size error is preferable for troubleshooting over determining
// why an execution environment may have terminated the process via its
// memory management processes, such as oom-killer on Linux.
//
// This value is kept as constant over allowing server configurability
// since there are many factors that influence message size, such as
// Terraform configuration and state data. If larger message size use
// cases appear, other gRPC options should be explored, such as
// implementing streaming RPCs and messages.
grpcMaxMessageSize = 256 << 20
)

// ServeOpt is an interface for defining options that can be passed to the
// Serve function. Each implementation modifies the ServeConfig being
// generated. A slice of ServeOpts then, cumulatively applied, render a full
Expand Down Expand Up @@ -240,7 +261,12 @@ func Serve(name string, serverFactory func() tfprotov5.ProviderServer, opts ...S
GRPCProvider: serverFactory,
},
},
GRPCServer: plugin.DefaultGRPCServer,
GRPCServer: func(opts []grpc.ServerOption) *grpc.Server {
opts = append(opts, grpc.MaxRecvMsgSize(grpcMaxMessageSize))
opts = append(opts, grpc.MaxSendMsgSize(grpcMaxMessageSize))

return grpc.NewServer(opts...)
},
}

if conf.logger != nil {
Expand Down
28 changes: 27 additions & 1 deletion tfprotov6/tf6server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/fromproto"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6"
"github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/toproto"
"google.golang.org/grpc"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/go-plugin"
Expand Down Expand Up @@ -59,6 +60,26 @@ const (
envTfReattachProviders = "TF_REATTACH_PROVIDERS"
)

const (
// grpcMaxMessageSize is the maximum gRPC send and receive message sizes
// for the server.
//
// This 256MB value is arbitrarily raised from the default message sizes of
// 4MB to account for advanced use cases, but arbitrarily lowered from
// MaxInt32 (or similar) to prevent incorrect server implementations from
// exhausting resources in common execution environments. Receiving a gRPC
// message size error is preferable for troubleshooting over determining
// why an execution environment may have terminated the process via its
// memory management processes, such as oom-killer on Linux.
//
// This value is kept as constant over allowing server configurability
// since there are many factors that influence message size, such as
// Terraform configuration and state data. If larger message size use
// cases appear, other gRPC options should be explored, such as
// implementing streaming RPCs and messages.
grpcMaxMessageSize = 256 << 20
)

// ServeOpt is an interface for defining options that can be passed to the
// Serve function. Each implementation modifies the ServeConfig being
// generated. A slice of ServeOpts then, cumulatively applied, render a full
Expand Down Expand Up @@ -242,7 +263,12 @@ func Serve(name string, serverFactory func() tfprotov6.ProviderServer, opts ...S
Name: name,
},
},
GRPCServer: plugin.DefaultGRPCServer,
GRPCServer: func(opts []grpc.ServerOption) *grpc.Server {
opts = append(opts, grpc.MaxRecvMsgSize(grpcMaxMessageSize))
opts = append(opts, grpc.MaxSendMsgSize(grpcMaxMessageSize))

return grpc.NewServer(opts...)
},
}

if conf.logger != nil {
Expand Down