Skip to content

Commit 30453ad

Browse files
gudvinrfguillot
authored andcommitted
refactor(fetcher): use time.Duration for client timeout values
All functions use time.Duration, so instead of converting everywhere, do it once.
1 parent 71af68b commit 30453ad

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

internal/config/config_test.go

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,12 +1607,22 @@ func TestMediaProxyHTTPClientTimeout(t *testing.T) {
16071607
t.Fatalf(`Parsing failure: %v`, err)
16081608
}
16091609

1610-
expected := 24
1610+
expected := 24 * time.Second
16111611
result := opts.MediaProxyHTTPClientTimeout()
16121612

16131613
if result != expected {
16141614
t.Fatalf(`Unexpected MEDIA_PROXY_HTTP_CLIENT_TIMEOUT value, got %d instead of %d`, result, expected)
16151615
}
1616+
1617+
sorted := opts.SortedOptions(false)
1618+
i := slices.IndexFunc(sorted, func(opt *option) bool {
1619+
return opt.Key == "MEDIA_PROXY_HTTP_CLIENT_TIMEOUT"
1620+
})
1621+
1622+
expectedSerialized := 24
1623+
if got := sorted[i].Value; got != expectedSerialized {
1624+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
1625+
}
16161626
}
16171627

16181628
func TestDefaultMediaProxyHTTPClientTimeoutValue(t *testing.T) {
@@ -1630,6 +1640,16 @@ func TestDefaultMediaProxyHTTPClientTimeoutValue(t *testing.T) {
16301640
if result != expected {
16311641
t.Fatalf(`Unexpected MEDIA_PROXY_HTTP_CLIENT_TIMEOUT value, got %d instead of %d`, result, expected)
16321642
}
1643+
1644+
sorted := opts.SortedOptions(false)
1645+
i := slices.IndexFunc(sorted, func(opt *option) bool {
1646+
return opt.Key == "MEDIA_PROXY_HTTP_CLIENT_TIMEOUT"
1647+
})
1648+
1649+
expectedSerialized := int(defaultMediaProxyHTTPClientTimeout / time.Second)
1650+
if got := sorted[i].Value; got != expectedSerialized {
1651+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
1652+
}
16331653
}
16341654

16351655
func TestMediaProxyCustomURL(t *testing.T) {
@@ -1706,12 +1726,22 @@ func TestHTTPClientTimeout(t *testing.T) {
17061726
t.Fatalf(`Parsing failure: %v`, err)
17071727
}
17081728

1709-
expected := 42
1729+
expected := 42 * time.Second
17101730
result := opts.HTTPClientTimeout()
17111731

17121732
if result != expected {
17131733
t.Fatalf(`Unexpected HTTP_CLIENT_TIMEOUT value, got %d instead of %d`, result, expected)
17141734
}
1735+
1736+
sorted := opts.SortedOptions(false)
1737+
i := slices.IndexFunc(sorted, func(opt *option) bool {
1738+
return opt.Key == "HTTP_CLIENT_TIMEOUT"
1739+
})
1740+
1741+
expectedSerialized := 42
1742+
if got := sorted[i].Value; got != expectedSerialized {
1743+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
1744+
}
17151745
}
17161746

17171747
func TestDefaultHTTPClientTimeoutValue(t *testing.T) {

internal/config/options.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const (
5252
defaultCleanupArchiveUnreadDays = 180
5353
defaultCleanupArchiveBatchSize = 10000
5454
defaultCleanupRemoveSessionsDays = 30
55-
defaultMediaProxyHTTPClientTimeout = 120
55+
defaultMediaProxyHTTPClientTimeout = 120 * time.Second
5656
defaultMediaProxyMode = "http-only"
5757
defaultMediaResourceTypes = "image"
5858
defaultMediaProxyURL = ""
@@ -74,7 +74,7 @@ const (
7474
defaultOauth2OidcProviderName = "OpenID Connect"
7575
defaultOAuth2Provider = ""
7676
defaultDisableLocalAuth = false
77-
defaultHTTPClientTimeout = 20
77+
defaultHTTPClientTimeout = 20 * time.Second
7878
defaultHTTPClientMaxBodySize = 15
7979
defaultHTTPClientProxy = ""
8080
defaultHTTPServerTimeout = 300 * time.Second
@@ -145,7 +145,7 @@ type options struct {
145145
createAdmin bool
146146
adminUsername string
147147
adminPassword string
148-
mediaProxyHTTPClientTimeout int
148+
mediaProxyHTTPClientTimeout time.Duration
149149
mediaProxyMode string
150150
mediaProxyResourceTypes []string
151151
mediaProxyCustomURL *url.URL
@@ -165,7 +165,7 @@ type options struct {
165165
oidcProviderName string
166166
oauth2Provider string
167167
disableLocalAuth bool
168-
httpClientTimeout int
168+
httpClientTimeout time.Duration
169169
httpClientMaxBodySize int64
170170
httpClientProxyURL *url.URL
171171
httpClientProxies []string
@@ -567,8 +567,8 @@ func (o *options) MediaCustomProxyURL() *url.URL {
567567
return o.mediaProxyCustomURL
568568
}
569569

570-
// MediaProxyHTTPClientTimeout returns the time limit in seconds before the proxy HTTP client cancel the request.
571-
func (o *options) MediaProxyHTTPClientTimeout() int {
570+
// MediaProxyHTTPClientTimeout returns the time limit before the proxy HTTP client cancel the request.
571+
func (o *options) MediaProxyHTTPClientTimeout() time.Duration {
572572
return o.mediaProxyHTTPClientTimeout
573573
}
574574

@@ -588,7 +588,7 @@ func (o *options) HasSchedulerService() bool {
588588
}
589589

590590
// HTTPClientTimeout returns the time limit in seconds before the HTTP client cancel the request.
591-
func (o *options) HTTPClientTimeout() int {
591+
func (o *options) HTTPClientTimeout() time.Duration {
592592
return o.httpClientTimeout
593593
}
594594

@@ -743,7 +743,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
743743
"HTTP_CLIENT_MAX_BODY_SIZE": o.httpClientMaxBodySize,
744744
"HTTP_CLIENT_PROXIES": clientProxyURLsRedacted,
745745
"HTTP_CLIENT_PROXY": clientProxyURLRedacted,
746-
"HTTP_CLIENT_TIMEOUT": o.httpClientTimeout,
746+
"HTTP_CLIENT_TIMEOUT": int(o.httpClientTimeout.Seconds()),
747747
"HTTP_CLIENT_USER_AGENT": o.httpClientUserAgent,
748748
"HTTP_SERVER_TIMEOUT": int(o.httpServerTimeout.Seconds()),
749749
"HTTP_SERVICE": o.httpService,
@@ -774,7 +774,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
774774
"POLLING_LIMIT_PER_HOST": o.pollingLimitPerHost,
775775
"POLLING_PARSING_ERROR_LIMIT": o.pollingParsingErrorLimit,
776776
"POLLING_SCHEDULER": o.pollingScheduler,
777-
"MEDIA_PROXY_HTTP_CLIENT_TIMEOUT": o.mediaProxyHTTPClientTimeout,
777+
"MEDIA_PROXY_HTTP_CLIENT_TIMEOUT": int(o.mediaProxyHTTPClientTimeout.Seconds()),
778778
"MEDIA_PROXY_RESOURCE_TYPES": o.mediaProxyResourceTypes,
779779
"MEDIA_PROXY_MODE": o.mediaProxyMode,
780780
"MEDIA_PROXY_PRIVATE_KEY": mediaProxyPrivateKeyValue,

internal/config/parser.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (p *parser) parseLines(lines []string) (err error) {
161161
case "SCHEDULER_ROUND_ROBIN_MAX_INTERVAL":
162162
p.opts.schedulerRoundRobinMaxInterval = parseInterval(value, time.Minute, defaultSchedulerRoundRobinMaxInterval)
163163
case "MEDIA_PROXY_HTTP_CLIENT_TIMEOUT":
164-
p.opts.mediaProxyHTTPClientTimeout = parseInt(value, defaultMediaProxyHTTPClientTimeout)
164+
p.opts.mediaProxyHTTPClientTimeout = parseInterval(value, time.Second, defaultMediaProxyHTTPClientTimeout)
165165
case "MEDIA_PROXY_MODE":
166166
p.opts.mediaProxyMode = parseString(value, defaultMediaProxyMode)
167167
case "MEDIA_PROXY_RESOURCE_TYPES":
@@ -206,7 +206,7 @@ func (p *parser) parseLines(lines []string) (err error) {
206206
case "DISABLE_LOCAL_AUTH":
207207
p.opts.disableLocalAuth = parseBool(value, defaultDisableLocalAuth)
208208
case "HTTP_CLIENT_TIMEOUT":
209-
p.opts.httpClientTimeout = parseInt(value, defaultHTTPClientTimeout)
209+
p.opts.httpClientTimeout = parseInterval(value, time.Second, defaultHTTPClientTimeout)
210210
case "HTTP_CLIENT_MAX_BODY_SIZE":
211211
p.opts.httpClientMaxBodySize = int64(parseInt(value, defaultHTTPClientMaxBodySize) * 1024 * 1024)
212212
case "HTTP_CLIENT_PROXY":

internal/reader/fetcher/request_builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import (
1818
)
1919

2020
const (
21-
defaultHTTPClientTimeout = 20
21+
defaultHTTPClientTimeout = 20 * time.Second
2222
defaultAcceptHeader = "application/xml, application/atom+xml, application/rss+xml, application/rdf+xml, application/feed+json, text/html, */*;q=0.9"
2323
)
2424

2525
type RequestBuilder struct {
2626
headers http.Header
2727
clientProxyURL *url.URL
28-
clientTimeout int
28+
clientTimeout time.Duration
2929
useClientProxy bool
3030
withoutRedirects bool
3131
ignoreTLSErrors bool
@@ -104,7 +104,7 @@ func (r *RequestBuilder) WithCustomFeedProxyURL(proxyURL string) *RequestBuilder
104104
return r
105105
}
106106

107-
func (r *RequestBuilder) WithTimeout(timeout int) *RequestBuilder {
107+
func (r *RequestBuilder) WithTimeout(timeout time.Duration) *RequestBuilder {
108108
r.clientTimeout = timeout
109109
return r
110110
}
@@ -185,7 +185,7 @@ func (r *RequestBuilder) ExecuteRequest(requestURL string) (*http.Response, erro
185185
}
186186

187187
client := &http.Client{
188-
Timeout: time.Duration(r.clientTimeout) * time.Second,
188+
Timeout: r.clientTimeout,
189189
}
190190

191191
if r.withoutRedirects {

internal/reader/fetcher/request_builder_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ func TestRequestBuilder_CustomAcceptHeaderNotOverridden(t *testing.T) {
232232

233233
func TestRequestBuilder_WithTimeout(t *testing.T) {
234234
builder := NewRequestBuilder()
235-
builder = builder.WithTimeout(30)
235+
builder = builder.WithTimeout(30 * time.Second)
236236

237-
if builder.clientTimeout != 30 {
237+
if builder.clientTimeout != 30*time.Second {
238238
t.Errorf("Expected timeout to be 30, got %d", builder.clientTimeout)
239239
}
240240
}
@@ -382,9 +382,8 @@ func TestRequestBuilder_ChainedMethods(t *testing.T) {
382382
WithUserAgent("TestAgent/1.0", "DefaultAgent/1.0").
383383
WithCookie("test=value").
384384
WithETag("etag123").
385-
WithTimeout(10).
385+
WithTimeout(10 * time.Second).
386386
ExecuteRequest(server.URL)
387-
388387
if err != nil {
389388
t.Fatalf("Expected no error, got %v", err)
390389
}
@@ -409,7 +408,7 @@ func TestRequestBuilder_TimeoutConfiguration(t *testing.T) {
409408

410409
builder := NewRequestBuilder()
411410
start := time.Now()
412-
_, err := builder.WithTimeout(1).ExecuteRequest(server.URL)
411+
_, err := builder.WithTimeout(1 * time.Second).ExecuteRequest(server.URL)
413412
duration := time.Since(start)
414413

415414
if err == nil {

0 commit comments

Comments
 (0)