Skip to content

Commit

Permalink
RHINENG-2433: limit number of server connections
Browse files Browse the repository at this point in the history
  • Loading branch information
psegedy committed Oct 19, 2023
1 parent 3dc903c commit 2d4769e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Config struct {
ResponseTimeout time.Duration
MaxRequestBodySize int64
MaxHeaderCount int
MaxGinConnections int

// kafka
KafkaAddress string
Expand Down Expand Up @@ -172,6 +173,7 @@ func initAPIromClowder() {
Cfg.ResponseTimeout = time.Duration(GetIntEnvOrDefault("RESPONSE_TIMEOUT", 60))
Cfg.MaxRequestBodySize = GetInt64EnvOrDefault("MAX_REQUEST_BODY_SIZE", 1*1024*1024)
Cfg.MaxHeaderCount = GetIntEnvOrDefault("MAX_HEADER_COUNT", 50)
Cfg.MaxGinConnections = GetIntEnvOrDefault("MAX_GIN_CONNECTIONS", 50)
}

func initKafkaFromClowder() {
Expand Down
2 changes: 2 additions & 0 deletions deploy/clowdapp.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ objects:
- {name: ENABLE_SATELLITE_FUNCTIONALITY, value: '${ENABLE_SATELLITE_FUNCTIONALITY}'}
- {name: MAX_REQUEST_BODY_SIZE, value: '${MAX_REQUEST_BODY_SIZE}'}
- {name: MAX_HEADER_COUNT, value: '${MAX_HEADER_COUNT}'}
- {name: MAX_GIN_CONNECTIONS, value: '${MAX_GIN_CONNECTIONS}'}

resources:
limits: {cpu: '${RES_LIMIT_CPU_MANAGER}', memory: '${RES_LIMIT_MEM_MANAGER}'}
Expand Down Expand Up @@ -581,6 +582,7 @@ parameters:
- {name: GOMEMLIMIT_MANAGER, value: '230MiB'} # set to 90% of the default memory limit 256Mi (don't forget `B`)
- {name: MAX_REQUEST_BODY_SIZE, value: '1048576'} # limit request body size, in bytes (default 1MB)
- {name: MAX_HEADER_COUNT, value: '50'} # limit number of request headers
- {name: MAX_GIN_CONNECTIONS, value: '50'}

# Listener
- {name: REPLICAS_LISTENER, value: '1'}
Expand Down
1 change: 1 addition & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func RunManager() {
// middlewares
app.Use(gin.Recovery())
middlewares.Prometheus().Use(app)
app.Use(middlewares.MaxConnections(utils.Cfg.MaxGinConnections))
app.Use(middlewares.RequestResponseLogger())
app.Use(gzip.Gzip(gzip.DefaultCompression))
endpointsConfig := getEndpointsConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ func LimitRequestHeaders(maxHeaderCount int) gin.HandlerFunc {
}
}
}

func MaxConnections(max int) gin.HandlerFunc {
conns := make(chan struct{}, max)
return func(c *gin.Context) {
conns <- struct{}{}
defer func() { <-conns }()
c.Next()
}
}

0 comments on commit 2d4769e

Please sign in to comment.