Return original error on resources.GetRemote retry timeouts

See #11327
This commit is contained in:
Bjørn Erik Pedersen 2023-08-04 18:46:47 +02:00
parent 16da1ade70
commit 22861cb4dc
2 changed files with 39 additions and 17 deletions

View file

@ -21,6 +21,7 @@ import (
"strings" "strings"
"testing" "testing"
qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib" "github.com/gohugoio/hugo/hugolib"
) )
@ -67,10 +68,10 @@ func TestGetRemoteRetry(t *testing.T) {
t.Parallel() t.Parallel()
temporaryHTTPCodes := []int{408, 429, 500, 502, 503, 504} temporaryHTTPCodes := []int{408, 429, 500, 502, 503, 504}
numPages := 30 numPages := 20
handler := func(w http.ResponseWriter, r *http.Request) { handler := func(w http.ResponseWriter, r *http.Request) {
if rand.Intn(4) == 0 { if rand.Intn(3) == 0 {
w.WriteHeader(temporaryHTTPCodes[rand.Intn(len(temporaryHTTPCodes))]) w.WriteHeader(temporaryHTTPCodes[rand.Intn(len(temporaryHTTPCodes))])
return return
} }
@ -81,9 +82,10 @@ func TestGetRemoteRetry(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(handler)) srv := httptest.NewServer(http.HandlerFunc(handler))
t.Cleanup(func() { srv.Close() }) t.Cleanup(func() { srv.Close() })
files := ` filesTemplate := `
-- hugo.toml -- -- hugo.toml --
disableKinds = ["home", "taxonomy", "term"] disableKinds = ["home", "taxonomy", "term"]
timeout = "TIMEOUT"
[security] [security]
[security.http] [security.http]
urls = ['.*'] urls = ['.*']
@ -93,7 +95,7 @@ mediaTypes = ['text/plain']
{{ $opts := dict }} {{ $opts := dict }}
{{ with resources.GetRemote $url $opts }} {{ with resources.GetRemote $url $opts }}
{{ with .Err }} {{ with .Err }}
{{ errorf "Unable to get remote resource: %s" . }} {{ errorf "Got Err: %s. Data: %v" . .Data }}
{{ else }} {{ else }}
Content: {{ .Content }} Content: {{ .Content }}
{{ end }} {{ end }}
@ -103,11 +105,13 @@ mediaTypes = ['text/plain']
` `
for i := 0; i < numPages; i++ { for i := 0; i < numPages; i++ {
files += fmt.Sprintf("-- content/post/p%d.md --\n", i) filesTemplate += fmt.Sprintf("-- content/post/p%d.md --\n", i)
} }
files = strings.ReplaceAll(files, "URL", srv.URL) filesTemplate = strings.ReplaceAll(filesTemplate, "URL", srv.URL)
t.Run("OK", func(t *testing.T) {
files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "60s")
b := hugolib.NewIntegrationTestBuilder( b := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{ hugolib.IntegrationTestConfig{
T: t, T: t,
@ -120,5 +124,22 @@ mediaTypes = ['text/plain']
for i := 0; i < numPages; i++ { for i := 0; i < numPages; i++ {
b.AssertFileContent(fmt.Sprintf("public/post/p%d/index.html", i), fmt.Sprintf("Content: Response for /post/p%d/.", i)) b.AssertFileContent(fmt.Sprintf("public/post/p%d/index.html", i), fmt.Sprintf("Content: Response for /post/p%d/.", i))
} }
})
t.Run("Timeout", func(t *testing.T) {
files := strings.ReplaceAll(filesTemplate, "TIMEOUT", "100ms")
b, err := hugolib.NewIntegrationTestBuilder(
hugolib.IntegrationTestConfig{
T: t,
TxtarString: files,
},
).BuildE()
b.Assert(err, qt.IsNotNil)
b.AssertLogContains("Got Err")
b.AssertLogContains("Retry timeout")
b.AssertLogContains("ContentLength:0")
})
} }

View file

@ -159,7 +159,8 @@ func (c *Client) FromRemote(uri string, optionsm map[string]any) (resource.Resou
if start.IsZero() { if start.IsZero() {
start = time.Now() start = time.Now()
} else if d := time.Since(start) + nextSleep; d >= c.rs.Cfg.Timeout() { } else if d := time.Since(start) + nextSleep; d >= c.rs.Cfg.Timeout() {
return nil, fmt.Errorf("timeout (configured to %s) fetching remote resource %s: last error: %w", c.rs.Cfg.Timeout(), uri, err) c.rs.Logger.Errorf("Retry timeout (configured to %s) fetching remote resource.", c.rs.Cfg.Timeout())
return nil, err
} }
time.Sleep(nextSleep) time.Sleep(nextSleep)
if nextSleep < nextSleepLimit { if nextSleep < nextSleepLimit {