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

Allow maximum request timeout to be larger than write timeout (avoid EOF) #31

Merged
merged 1 commit into from
Aug 11, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions pkg/httpserver/httpserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,21 @@ type httpServer struct {
l net.Listener
conf config.Section
corsConf config.Section
options ServerOptions
onClose chan error
tlsEnabled bool
tlsCertFile string
tlsKeyFile string
shutdownTimeout time.Duration
}

func NewHTTPServer(ctx context.Context, name string, r *mux.Router, onClose chan error, conf config.Section, corsConf config.Section) (is HTTPServer, err error) {
// ServerOptions are config parameters that are not set from the config, but rather the context
// in which the HTTP server is being used
type ServerOptions struct {
MaximumRequestTimeout time.Duration
}

func NewHTTPServer(ctx context.Context, name string, r *mux.Router, onClose chan error, conf config.Section, corsConf config.Section, opts ...*ServerOptions) (is HTTPServer, err error) {
hs := &httpServer{
name: name,
onClose: onClose,
Expand All @@ -69,6 +76,9 @@ func NewHTTPServer(ctx context.Context, name string, r *mux.Router, onClose chan
tlsKeyFile: conf.GetString(HTTPConfTLSKeyFile),
shutdownTimeout: conf.GetDuration(HTTPConfShutdownTimeout),
}
for _, o := range opts {
hs.options = *o
}
hs.l, err = hs.createListener(ctx)
if err == nil {
hs.s, err = hs.createServer(ctx, r)
Expand Down Expand Up @@ -126,11 +136,25 @@ func (hs *httpServer) createServer(ctx context.Context, r *mux.Router) (srv *htt
return nil, err
}
handler = wrapCorsIfEnabled(ctx, hs.corsConf, handler)

// Where a maximum request timeout is set, it does not make sense for either the
// read timeout (time to read full body), or the write timeout (time to write the
// response after processing the request) to be less than that
readTimeout := hs.conf.GetDuration(HTTPConfReadTimeout)
if readTimeout < hs.options.MaximumRequestTimeout {
readTimeout = hs.options.MaximumRequestTimeout + 1*time.Second
}
writeTimeout := hs.conf.GetDuration(HTTPConfWriteTimeout)
if writeTimeout < hs.options.MaximumRequestTimeout {
writeTimeout = hs.options.MaximumRequestTimeout + 1*time.Second
}

log.L(ctx).Debugf("HTTP Server Timeouts (%s): read=%s write=%s request=%s", hs.l.Addr(), readTimeout, writeTimeout, hs.options.MaximumRequestTimeout)
srv = &http.Server{
Handler: handler,
WriteTimeout: hs.conf.GetDuration(HTTPConfWriteTimeout),
ReadTimeout: hs.conf.GetDuration(HTTPConfReadTimeout),
ReadHeaderTimeout: hs.conf.GetDuration(HTTPConfReadTimeout),
WriteTimeout: writeTimeout,
ReadTimeout: readTimeout,
ReadHeaderTimeout: hs.conf.GetDuration(HTTPConfReadTimeout), // safe for this to always be the read timeout - should be short
TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
ClientAuth: clientAuth,
Expand Down
7 changes: 5 additions & 2 deletions pkg/httpserver/httpserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,20 @@ func TestServeFail(t *testing.T) {
assert.Error(t, err)
}

func TestShutdownOk(t *testing.T) {
func TestShutdownOkCustomOpts(t *testing.T) {
config.RootConfigReset()
cp := config.RootSection("ut")
InitHTTPConfig(cp, 0)
cc := config.RootSection("utCors")
InitCORSConfig(cc)
errChan := make(chan error)
ctx, cancel := context.WithCancel(context.Background())
l, err := NewHTTPServer(ctx, "ut", mux.NewRouter(), errChan, cp, cc)
l, err := NewHTTPServer(ctx, "ut", mux.NewRouter(), errChan, cp, cc, &ServerOptions{
MaximumRequestTimeout: 1 * time.Hour,
})
assert.NoError(t, err)
assert.NotEmpty(t, l.Addr().String())
assert.Equal(t, 1*time.Hour, l.(*httpServer).options.MaximumRequestTimeout)
cancel()
}

Expand Down