Disable keepalive connections and set idle timeout to 10 seconds, explicitly

This commit is contained in:
eleksir
2026-04-04 16:17:21 +03:00
parent ae321a216d
commit f20f490c15
2 changed files with 15 additions and 4 deletions

View File

@@ -14,6 +14,9 @@ const (
// maxHeaderBytes is the maximum size of request headers (1KB). // maxHeaderBytes is the maximum size of request headers (1KB).
maxHeaderBytes = 1024 maxHeaderBytes = 1024
// idleTimeout is the maximum time to wait for the next request on keep-alive connections.
idleTimeout = 10 * time.Second
// portMin is the minimum valid port number. // portMin is the minimum valid port number.
portMin = 1 portMin = 1

View File

@@ -178,31 +178,39 @@ func NewServer(configPath string) (*ServerWrapper, error) {
// HTTP mode if nohttps is enabled in config. // HTTP mode if nohttps is enabled in config.
// HTTPS mode if nohttps is disabled in config. // HTTPS mode if nohttps is disabled in config.
if C.NoHTTPS { if C.NoHTTPS {
return &ServerWrapper{ srv := &ServerWrapper{
Server: &http.Server{ Server: &http.Server{
Addr: fmt.Sprintf("%s:%d", C.Address, C.Port), Addr: fmt.Sprintf("%s:%d", C.Address, C.Port),
Handler: withServerHeader(mux), Handler: withServerHeader(mux),
ReadHeaderTimeout: readHeaderTimeout, ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: time.Duration(C.BackupTimeout) * time.Minute, WriteTimeout: time.Duration(C.BackupTimeout) * time.Minute,
IdleTimeout: idleTimeout,
MaxHeaderBytes: maxHeaderBytes, MaxHeaderBytes: maxHeaderBytes,
ErrorLog: log.DebugLogger(), ErrorLog: log.DebugLogger(),
}, },
}, nil }
srv.SetKeepAlivesEnabled(false)
return srv, nil
} }
return &ServerWrapper{ srv := &ServerWrapper{
Server: &http.Server{ Server: &http.Server{
Addr: fmt.Sprintf("%s:%d", C.Address, C.Port), Addr: fmt.Sprintf("%s:%d", C.Address, C.Port),
Handler: withServerHeader(mux), Handler: withServerHeader(mux),
ReadHeaderTimeout: readHeaderTimeout, ReadHeaderTimeout: readHeaderTimeout,
WriteTimeout: time.Duration(C.BackupTimeout) * time.Minute, WriteTimeout: time.Duration(C.BackupTimeout) * time.Minute,
IdleTimeout: idleTimeout,
MaxHeaderBytes: maxHeaderBytes, MaxHeaderBytes: maxHeaderBytes,
TLSConfig: &tls.Config{ TLSConfig: &tls.Config{
MinVersion: tls.VersionTLS13, MinVersion: tls.VersionTLS13,
}, },
ErrorLog: log.DebugLogger(), ErrorLog: log.DebugLogger(),
}, },
}, nil }
srv.SetKeepAlivesEnabled(false)
return srv, nil
} }
// writeWithContext checks if context is canceled before executing fn. // writeWithContext checks if context is canceled before executing fn.