gRPC Server: Make message size limits configurable. (#86982)

* gRPC Server: Make message size limits configurable.

* Fix mistake, don't add opts twice

* Apply suggestions from code review

Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>

---------

Co-authored-by: Todd Treece <360020+toddtreece@users.noreply.github.com>
This commit is contained in:
Steve Simpson 2024-04-30 16:18:03 +02:00 committed by GitHub
parent 4cc6b53a6d
commit 5c89b8fe12
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 25 additions and 5 deletions

View File

@ -107,6 +107,12 @@ key_file =
# this will log the request and response for each unary gRPC call
enable_logging = false
# Maximum size of a message that can be received in bytes. If not set, uses the gRPC default (4MiB).
max_recv_msg_size =
# Maximum size of a message that can be sent in bytes. If not set, uses the gRPC default (unlimited).
max_send_msg_size =
#################################### Database ############################
[database]
# You can configure the database connection by specifying type, host, name, user and password

View File

@ -101,6 +101,8 @@
;use_tls = false
;cert_file =
;key_file =
;max_recv_msg_size =
;max_send_msg_size =
#################################### Database ####################################
[database]

View File

@ -85,12 +85,20 @@ func ProvideService(cfg *setting.Cfg, features featuremgmt.FeatureToggles, authe
opts = append(opts, grpc.Creds(credentials.NewTLS(cfg.GRPCServerTLSConfig)))
}
if s.cfg.GRPCServerMaxRecvMsgSize > 0 {
opts = append(opts, grpc.MaxRecvMsgSize(s.cfg.GRPCServerMaxRecvMsgSize))
}
if s.cfg.GRPCServerMaxSendMsgSize > 0 {
opts = append(opts, grpc.MaxSendMsgSize(s.cfg.GRPCServerMaxSendMsgSize))
}
s.server = grpc.NewServer(opts...)
return s, nil
}
func (s *gPRCServerService) Run(ctx context.Context) error {
s.logger.Info("Running GRPC server", "address", s.cfg.GRPCServerAddress, "network", s.cfg.GRPCServerNetwork, "tls", s.cfg.GRPCServerTLSConfig != nil)
s.logger.Info("Running GRPC server", "address", s.cfg.GRPCServerAddress, "network", s.cfg.GRPCServerNetwork, "tls", s.cfg.GRPCServerTLSConfig != nil, "max_recv_msg_size", s.cfg.GRPCServerMaxRecvMsgSize, "max_send_msg_size", s.cfg.GRPCServerMaxSendMsgSize)
listener, err := net.Listen(s.cfg.GRPCServerNetwork, s.cfg.GRPCServerAddress)
if err != nil {

View File

@ -470,10 +470,12 @@ type Cfg struct {
RBACSingleOrganization bool
// GRPC Server.
GRPCServerNetwork string
GRPCServerAddress string
GRPCServerTLSConfig *tls.Config
GRPCServerEnableLogging bool // log request and response of each unary gRPC call
GRPCServerNetwork string
GRPCServerAddress string
GRPCServerTLSConfig *tls.Config
GRPCServerEnableLogging bool // log request and response of each unary gRPC call
GRPCServerMaxRecvMsgSize int
GRPCServerMaxSendMsgSize int
CustomResponseHeaders map[string]string
@ -1769,6 +1771,8 @@ func readGRPCServerSettings(cfg *Cfg, iniFile *ini.File) error {
cfg.GRPCServerNetwork = valueAsString(server, "network", "tcp")
cfg.GRPCServerAddress = valueAsString(server, "address", "")
cfg.GRPCServerEnableLogging = server.Key("enable_logging").MustBool(false)
cfg.GRPCServerMaxRecvMsgSize = server.Key("max_recv_msg_size").MustInt(0)
cfg.GRPCServerMaxSendMsgSize = server.Key("max_send_msg_size").MustInt(0)
switch cfg.GRPCServerNetwork {
case "unix":
if cfg.GRPCServerAddress != "" {