Bläddra i källkod

refactor(server): use time.Duration for timeout values

Instead of converting at the very last moment,
it's simpler and more readable to use time.Duration ASAP.
gudvinr 7 månader sedan
förälder
incheckning
71af68becd

+ 11 - 1
internal/config/config_test.go

@@ -1776,12 +1776,22 @@ func TestHTTPServerTimeout(t *testing.T) {
 		t.Fatalf(`Parsing failure: %v`, err)
 	}
 
-	expected := 342
+	expected := 342 * time.Second
 	result := opts.HTTPServerTimeout()
 
 	if result != expected {
 		t.Fatalf(`Unexpected HTTP_SERVER_TIMEOUT value, got %d instead of %d`, result, expected)
 	}
+
+	sorted := opts.SortedOptions(false)
+	i := slices.IndexFunc(sorted, func(opt *option) bool {
+		return opt.Key == "HTTP_SERVER_TIMEOUT"
+	})
+
+	expectedSerialized := 342
+	if got := sorted[i].Value; got != expectedSerialized {
+		t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
+	}
 }
 
 func TestDefaultHTTPServerTimeoutValue(t *testing.T) {

+ 5 - 5
internal/config/options.go

@@ -77,7 +77,7 @@ const (
 	defaultHTTPClientTimeout                  = 20
 	defaultHTTPClientMaxBodySize              = 15
 	defaultHTTPClientProxy                    = ""
-	defaultHTTPServerTimeout                  = 300
+	defaultHTTPServerTimeout                  = 300 * time.Second
 	defaultAuthProxyHeader                    = ""
 	defaultAuthProxyUserCreation              = false
 	defaultMaintenanceMode                    = false
@@ -170,7 +170,7 @@ type options struct {
 	httpClientProxyURL                 *url.URL
 	httpClientProxies                  []string
 	httpClientUserAgent                string
-	httpServerTimeout                  int
+	httpServerTimeout                  time.Duration
 	authProxyHeader                    string
 	authProxyUserCreation              bool
 	maintenanceMode                    bool
@@ -617,8 +617,8 @@ func (o *options) HasHTTPClientProxiesConfigured() bool {
 	return len(o.httpClientProxies) > 0
 }
 
-// HTTPServerTimeout returns the time limit in seconds before the HTTP server cancel the request.
-func (o *options) HTTPServerTimeout() int {
+// HTTPServerTimeout returns the time limit before the HTTP server cancel the request.
+func (o *options) HTTPServerTimeout() time.Duration {
 	return o.httpServerTimeout
 }
 
@@ -745,7 +745,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
 		"HTTP_CLIENT_PROXY":                      clientProxyURLRedacted,
 		"HTTP_CLIENT_TIMEOUT":                    o.httpClientTimeout,
 		"HTTP_CLIENT_USER_AGENT":                 o.httpClientUserAgent,
-		"HTTP_SERVER_TIMEOUT":                    o.httpServerTimeout,
+		"HTTP_SERVER_TIMEOUT":                    int(o.httpServerTimeout.Seconds()),
 		"HTTP_SERVICE":                           o.httpService,
 		"INVIDIOUS_INSTANCE":                     o.invidiousInstance,
 		"KEY_FILE":                               o.certKeyFile,

+ 1 - 1
internal/config/parser.go

@@ -219,7 +219,7 @@ func (p *parser) parseLines(lines []string) (err error) {
 		case "HTTP_CLIENT_USER_AGENT":
 			p.opts.httpClientUserAgent = parseString(value, defaultHTTPClientUserAgent)
 		case "HTTP_SERVER_TIMEOUT":
-			p.opts.httpServerTimeout = parseInt(value, defaultHTTPServerTimeout)
+			p.opts.httpServerTimeout = parseInterval(value, time.Second, defaultHTTPServerTimeout)
 		case "AUTH_PROXY_HEADER":
 			p.opts.authProxyHeader = parseString(value, defaultAuthProxyHeader)
 		case "AUTH_PROXY_USER_CREATION":

+ 3 - 4
internal/http/server/httpd.go

@@ -12,7 +12,6 @@ import (
 	"os"
 	"strconv"
 	"strings"
-	"time"
 
 	"miniflux.app/v2/internal/api"
 	"miniflux.app/v2/internal/config"
@@ -67,9 +66,9 @@ func StartWebServer(store *storage.Storage, pool *worker.Pool) []*http.Server {
 
 	for i, listenAddr := range listenAddresses {
 		server := &http.Server{
-			ReadTimeout:  time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
-			WriteTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
-			IdleTimeout:  time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
+			ReadTimeout:  config.Opts.HTTPServerTimeout(),
+			WriteTimeout: config.Opts.HTTPServerTimeout(),
+			IdleTimeout:  config.Opts.HTTPServerTimeout(),
 			Handler:      setupHandler(store, pool),
 		}