Skip to content

Commit 71af68b

Browse files
gudvinrfguillot
authored andcommitted
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.
1 parent ed3bf59 commit 71af68b

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

internal/config/config_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1776,12 +1776,22 @@ func TestHTTPServerTimeout(t *testing.T) {
17761776
t.Fatalf(`Parsing failure: %v`, err)
17771777
}
17781778

1779-
expected := 342
1779+
expected := 342 * time.Second
17801780
result := opts.HTTPServerTimeout()
17811781

17821782
if result != expected {
17831783
t.Fatalf(`Unexpected HTTP_SERVER_TIMEOUT value, got %d instead of %d`, result, expected)
17841784
}
1785+
1786+
sorted := opts.SortedOptions(false)
1787+
i := slices.IndexFunc(sorted, func(opt *option) bool {
1788+
return opt.Key == "HTTP_SERVER_TIMEOUT"
1789+
})
1790+
1791+
expectedSerialized := 342
1792+
if got := sorted[i].Value; got != expectedSerialized {
1793+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
1794+
}
17851795
}
17861796

17871797
func TestDefaultHTTPServerTimeoutValue(t *testing.T) {

internal/config/options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const (
7777
defaultHTTPClientTimeout = 20
7878
defaultHTTPClientMaxBodySize = 15
7979
defaultHTTPClientProxy = ""
80-
defaultHTTPServerTimeout = 300
80+
defaultHTTPServerTimeout = 300 * time.Second
8181
defaultAuthProxyHeader = ""
8282
defaultAuthProxyUserCreation = false
8383
defaultMaintenanceMode = false
@@ -170,7 +170,7 @@ type options struct {
170170
httpClientProxyURL *url.URL
171171
httpClientProxies []string
172172
httpClientUserAgent string
173-
httpServerTimeout int
173+
httpServerTimeout time.Duration
174174
authProxyHeader string
175175
authProxyUserCreation bool
176176
maintenanceMode bool
@@ -617,8 +617,8 @@ func (o *options) HasHTTPClientProxiesConfigured() bool {
617617
return len(o.httpClientProxies) > 0
618618
}
619619

620-
// HTTPServerTimeout returns the time limit in seconds before the HTTP server cancel the request.
621-
func (o *options) HTTPServerTimeout() int {
620+
// HTTPServerTimeout returns the time limit before the HTTP server cancel the request.
621+
func (o *options) HTTPServerTimeout() time.Duration {
622622
return o.httpServerTimeout
623623
}
624624

@@ -745,7 +745,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
745745
"HTTP_CLIENT_PROXY": clientProxyURLRedacted,
746746
"HTTP_CLIENT_TIMEOUT": o.httpClientTimeout,
747747
"HTTP_CLIENT_USER_AGENT": o.httpClientUserAgent,
748-
"HTTP_SERVER_TIMEOUT": o.httpServerTimeout,
748+
"HTTP_SERVER_TIMEOUT": int(o.httpServerTimeout.Seconds()),
749749
"HTTP_SERVICE": o.httpService,
750750
"INVIDIOUS_INSTANCE": o.invidiousInstance,
751751
"KEY_FILE": o.certKeyFile,

internal/config/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (p *parser) parseLines(lines []string) (err error) {
219219
case "HTTP_CLIENT_USER_AGENT":
220220
p.opts.httpClientUserAgent = parseString(value, defaultHTTPClientUserAgent)
221221
case "HTTP_SERVER_TIMEOUT":
222-
p.opts.httpServerTimeout = parseInt(value, defaultHTTPServerTimeout)
222+
p.opts.httpServerTimeout = parseInterval(value, time.Second, defaultHTTPServerTimeout)
223223
case "AUTH_PROXY_HEADER":
224224
p.opts.authProxyHeader = parseString(value, defaultAuthProxyHeader)
225225
case "AUTH_PROXY_USER_CREATION":

internal/http/server/httpd.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"os"
1313
"strconv"
1414
"strings"
15-
"time"
1615

1716
"miniflux.app/v2/internal/api"
1817
"miniflux.app/v2/internal/config"
@@ -67,9 +66,9 @@ func StartWebServer(store *storage.Storage, pool *worker.Pool) []*http.Server {
6766

6867
for i, listenAddr := range listenAddresses {
6968
server := &http.Server{
70-
ReadTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
71-
WriteTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
72-
IdleTimeout: time.Duration(config.Opts.HTTPServerTimeout()) * time.Second,
69+
ReadTimeout: config.Opts.HTTPServerTimeout(),
70+
WriteTimeout: config.Opts.HTTPServerTimeout(),
71+
IdleTimeout: config.Opts.HTTPServerTimeout(),
7372
Handler: setupHandler(store, pool),
7473
}
7574

0 commit comments

Comments
 (0)