Skip to content

Feat/configurable server timeouts #1310

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions interceptor/config/timeouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Timeouts struct {
// after sending request headers if the server returned an Expect: 100-continue
// header
ExpectContinueTimeout time.Duration `envconfig:"KEDA_HTTP_EXPECT_CONTINUE_TIMEOUT" default:"1s"`
// ServerReadTimeout is the maximum duration for reading the entire request, including the body.
ServerReadTimeout time.Duration `envconfig:"KEDA_HTTP_SERVER_READ_TIMEOUT" default:"120s"`
// ServerWriteTimeout is the maximum duration before timing out writes of the response.
ServerWriteTimeout time.Duration `envconfig:"KEDA_HTTP_SERVER_WRITE_TIMEOUT" default:"120s"`
// ServerIdleTimeout is the maximum amount of time to wait for the next request when keep-alives are enabled.
ServerIdleTimeout time.Duration `envconfig:"KEDA_HTTP_SERVER_IDLE_TIMEOUT" default:"120s"`
Comment on lines +38 to +42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make the defaults be 0? That way, the default behaviour remains the same and it won't be a potentially breaking change.

}

// Backoff returns a wait.Backoff based on the timeouts in t
Expand Down
11 changes: 11 additions & 0 deletions interceptor/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,16 @@ func Validate(srvCfg *Serving, timeoutsCfg Timeouts, lggr logr.Logger) error {
endpointsCachePollInterval,
)
}

if timeoutsCfg.ServerReadTimeout < 0 {
return fmt.Errorf("timeout value KEDA_HTTP_SERVER_READ_TIMEOUT must not be negative")
}
if timeoutsCfg.ServerWriteTimeout < 0 {
return fmt.Errorf("timeout value KEDA_HTTP_SERVER_WRITE_TIMEOUT must not be negative")
}
if timeoutsCfg.ServerIdleTimeout < 0 {
return fmt.Errorf("timeout value KEDA_HTTP_SERVER_IDLE_TIMEOUT must not be negative")
}

return nil
}
4 changes: 2 additions & 2 deletions interceptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@

addr := fmt.Sprintf("0.0.0.0:%d", port)
lggr.Info("admin server starting", "address", addr)
return kedahttp.ServeContext(ctx, addr, adminServer, nil)

Check failure on line 256 in interceptor/main.go

View workflow job for this annotation

GitHub Actions / Static Checks

not enough arguments in call to kedahttp.ServeContext
}

func runMetricsServer(
Expand All @@ -263,7 +263,7 @@
) error {
lggr.Info("starting the prometheus metrics server", "port", metricsCfg.OtelPrometheusExporterPort, "path", "/metrics")
addr := fmt.Sprintf("0.0.0.0:%d", metricsCfg.OtelPrometheusExporterPort)
return kedahttp.ServeContext(ctx, addr, promhttp.Handler(), nil)

Check failure on line 266 in interceptor/main.go

View workflow job for this annotation

GitHub Actions / Static Checks

not enough arguments in call to kedahttp.ServeContext
}

// addCert adds a certificate to the map of certificates based on the certificate's SANs
Expand Down Expand Up @@ -470,7 +470,7 @@
addr := fmt.Sprintf("0.0.0.0:%d", port)
logger.Info("proxy server starting", "address", addr)
if tlsEnabled {
return kedahttp.ServeContext(ctx, addr, rootHandler, tlsCfg)
return kedahttp.ServeContext(ctx, addr, rootHandler, tlsCfg, timeouts)
}
return kedahttp.ServeContext(ctx, addr, rootHandler, nil)
return kedahttp.ServeContext(ctx, addr, rootHandler, nil, timeouts)
}
18 changes: 14 additions & 4 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,24 @@ import (
"crypto/tls"
"net/http"

"github.com/kedacore/http-add-on/interceptor/config"
"github.com/kedacore/http-add-on/pkg/util"
)

func ServeContext(ctx context.Context, addr string, hdl http.Handler, tlsConfig *tls.Config) error {
func ServeContext(
ctx context.Context,
addr string,
hdl http.Handler,
tlsConfig *tls.Config,
timeouts *config.Timeouts,
) error {
srv := &http.Server{
Handler: hdl,
Addr: addr,
TLSConfig: tlsConfig,
Handler: hdl,
Addr: addr,
TLSConfig: tlsConfig,
ReadTimeout: timeouts.ServerReadTimeout,
WriteTimeout: timeouts.ServerWriteTimeout,
IdleTimeout: timeouts.ServerIdleTimeout,
}

go func() {
Expand Down
Loading