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

ROX-20121: set gRPC max concurrent streams to 100 #1296

Merged
merged 3 commits into from
Oct 13, 2023
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
15 changes: 14 additions & 1 deletion api/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
grpcprometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
log "github.com/sirupsen/logrus"
"github.com/stackrox/scanner/pkg/env"
"github.com/stackrox/scanner/pkg/mtls"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand All @@ -27,6 +28,14 @@ func init() {
grpcprometheus.EnableHandlingTimeHistogram()
}

func maxGrpcConcurrentStreams() uint32 {
if env.MaxGrpcConcurrentStreams.Int() <= 0 {
return env.DefaultMaxGrpcConcurrentStreams
}

return uint32(env.MaxGrpcConcurrentStreams.Int())
}

// NewAPI creates a new gRPC API instantiation
func NewAPI(config Config) API {
return &apiImpl{
Expand Down Expand Up @@ -56,7 +65,11 @@ func (a *apiImpl) connectToLocalEndpoint() (*grpc.ClientConn, error) {
}

func (a *apiImpl) Start() {
grpcServer := grpc.NewServer(grpcmiddleware.WithUnaryServerChain(a.unaryInterceptors()...))
grpcServer := grpc.NewServer(
grpcmiddleware.WithUnaryServerChain(a.unaryInterceptors()...),
grpc.MaxConcurrentStreams(maxGrpcConcurrentStreams()),
)

for _, serv := range a.apiServices {
serv.RegisterServiceServer(grpcServer)
}
Expand Down
33 changes: 33 additions & 0 deletions pkg/env/integersetting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package env

import (
"strconv"
)

// IntegerSetting represents an environment variable which should be parsed into an integer.
type IntegerSetting interface {
Setting
Int() int
}

type integerSetting struct {
Setting
defaultValue int
}

// Int returns the int object represented by the environment variable.
func (s *integerSetting) Int() int {
v, err := strconv.Atoi(s.Value())
if err != nil {
return s.defaultValue
}
return v
}

// RegisterIntegerSetting globally registers and returns a new integer setting.
func RegisterIntegerSetting(envVar string, defaultValue int, opts ...SettingOption) IntegerSetting {
return &integerSetting{
Setting: registerSetting(envVar, append(opts, WithDefault(strconv.Itoa(defaultValue)))...),
defaultValue: defaultValue,
}
}
8 changes: 8 additions & 0 deletions pkg/env/list.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package env

const (
// DefaultMaxGrpcConcurrentStreams is the minimum value for concurrent streams recommended by the HTTP/2 spec
DefaultMaxGrpcConcurrentStreams = 100
)

var (
// LanguageVulns enables language vulnerabilities.
LanguageVulns = RegisterBooleanSetting("ROX_LANGUAGE_VULNS", true, AllowWithoutRox())
Expand All @@ -19,4 +24,7 @@ var (
// This is ignored if SkipPeerValidation is enabled.
// This variable was copied over from the stackrox repo.
OpenshiftAPI = RegisterBooleanSetting("ROX_OPENSHIFT_API", false)

// MaxGrpcConcurrentStreams configures the maximum number of HTTP/2 streams to use with gRPC
MaxGrpcConcurrentStreams = RegisterIntegerSetting("ROX_GRPC_MAX_CONCURRENT_STREAMS", DefaultMaxGrpcConcurrentStreams)
)