mirror of
https://github.com/miniflux/v2.git
synced 2025-08-26 18:21:01 +00:00
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.
This commit is contained in:
parent
ed3bf59356
commit
71af68becd
4 changed files with 20 additions and 11 deletions
|
@ -1776,12 +1776,22 @@ func TestHTTPServerTimeout(t *testing.T) {
|
||||||
t.Fatalf(`Parsing failure: %v`, err)
|
t.Fatalf(`Parsing failure: %v`, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
expected := 342
|
expected := 342 * time.Second
|
||||||
result := opts.HTTPServerTimeout()
|
result := opts.HTTPServerTimeout()
|
||||||
|
|
||||||
if result != expected {
|
if result != expected {
|
||||||
t.Fatalf(`Unexpected HTTP_SERVER_TIMEOUT value, got %d instead of %d`, 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) {
|
func TestDefaultHTTPServerTimeoutValue(t *testing.T) {
|
||||||
|
|
|
@ -77,7 +77,7 @@ const (
|
||||||
defaultHTTPClientTimeout = 20
|
defaultHTTPClientTimeout = 20
|
||||||
defaultHTTPClientMaxBodySize = 15
|
defaultHTTPClientMaxBodySize = 15
|
||||||
defaultHTTPClientProxy = ""
|
defaultHTTPClientProxy = ""
|
||||||
defaultHTTPServerTimeout = 300
|
defaultHTTPServerTimeout = 300 * time.Second
|
||||||
defaultAuthProxyHeader = ""
|
defaultAuthProxyHeader = ""
|
||||||
defaultAuthProxyUserCreation = false
|
defaultAuthProxyUserCreation = false
|
||||||
defaultMaintenanceMode = false
|
defaultMaintenanceMode = false
|
||||||
|
@ -170,7 +170,7 @@ type options struct {
|
||||||
httpClientProxyURL *url.URL
|
httpClientProxyURL *url.URL
|
||||||
httpClientProxies []string
|
httpClientProxies []string
|
||||||
httpClientUserAgent string
|
httpClientUserAgent string
|
||||||
httpServerTimeout int
|
httpServerTimeout time.Duration
|
||||||
authProxyHeader string
|
authProxyHeader string
|
||||||
authProxyUserCreation bool
|
authProxyUserCreation bool
|
||||||
maintenanceMode bool
|
maintenanceMode bool
|
||||||
|
@ -617,8 +617,8 @@ func (o *options) HasHTTPClientProxiesConfigured() bool {
|
||||||
return len(o.httpClientProxies) > 0
|
return len(o.httpClientProxies) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPServerTimeout returns the time limit in seconds before the HTTP server cancel the request.
|
// HTTPServerTimeout returns the time limit before the HTTP server cancel the request.
|
||||||
func (o *options) HTTPServerTimeout() int {
|
func (o *options) HTTPServerTimeout() time.Duration {
|
||||||
return o.httpServerTimeout
|
return o.httpServerTimeout
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -745,7 +745,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
|
||||||
"HTTP_CLIENT_PROXY": clientProxyURLRedacted,
|
"HTTP_CLIENT_PROXY": clientProxyURLRedacted,
|
||||||
"HTTP_CLIENT_TIMEOUT": o.httpClientTimeout,
|
"HTTP_CLIENT_TIMEOUT": o.httpClientTimeout,
|
||||||
"HTTP_CLIENT_USER_AGENT": o.httpClientUserAgent,
|
"HTTP_CLIENT_USER_AGENT": o.httpClientUserAgent,
|
||||||
"HTTP_SERVER_TIMEOUT": o.httpServerTimeout,
|
"HTTP_SERVER_TIMEOUT": int(o.httpServerTimeout.Seconds()),
|
||||||
"HTTP_SERVICE": o.httpService,
|
"HTTP_SERVICE": o.httpService,
|
||||||
"INVIDIOUS_INSTANCE": o.invidiousInstance,
|
"INVIDIOUS_INSTANCE": o.invidiousInstance,
|
||||||
"KEY_FILE": o.certKeyFile,
|
"KEY_FILE": o.certKeyFile,
|
||||||
|
|
|
@ -219,7 +219,7 @@ func (p *parser) parseLines(lines []string) (err error) {
|
||||||
case "HTTP_CLIENT_USER_AGENT":
|
case "HTTP_CLIENT_USER_AGENT":
|
||||||
p.opts.httpClientUserAgent = parseString(value, defaultHTTPClientUserAgent)
|
p.opts.httpClientUserAgent = parseString(value, defaultHTTPClientUserAgent)
|
||||||
case "HTTP_SERVER_TIMEOUT":
|
case "HTTP_SERVER_TIMEOUT":
|
||||||
p.opts.httpServerTimeout = parseInt(value, defaultHTTPServerTimeout)
|
p.opts.httpServerTimeout = parseInterval(value, time.Second, defaultHTTPServerTimeout)
|
||||||
case "AUTH_PROXY_HEADER":
|
case "AUTH_PROXY_HEADER":
|
||||||
p.opts.authProxyHeader = parseString(value, defaultAuthProxyHeader)
|
p.opts.authProxyHeader = parseString(value, defaultAuthProxyHeader)
|
||||||
case "AUTH_PROXY_USER_CREATION":
|
case "AUTH_PROXY_USER_CREATION":
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"miniflux.app/v2/internal/api"
|
"miniflux.app/v2/internal/api"
|
||||||
"miniflux.app/v2/internal/config"
|
"miniflux.app/v2/internal/config"
|
||||||
|
@ -67,9 +66,9 @@ func StartWebServer(store *storage.Storage, pool *worker.Pool) []*http.Server {
|
||||||
|
|
||||||
for i, listenAddr := range listenAddresses {
|
for i, listenAddr := range listenAddresses {
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
ReadTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
|
ReadTimeout: config.Opts.HTTPServerTimeout(),
|
||||||
WriteTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
|
WriteTimeout: config.Opts.HTTPServerTimeout(),
|
||||||
IdleTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
|
IdleTimeout: config.Opts.HTTPServerTimeout(),
|
||||||
Handler: setupHandler(store, pool),
|
Handler: setupHandler(store, pool),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue