resources: Add timeout to the HTTP request in Get
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 2 Dec 2021 13:13:24 +0000 (14:13 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 2 Dec 2021 15:11:14 +0000 (16:11 +0100)
Workaround for https://github.com/golang/go/issues/49366

.gitignore
cache/filecache/filecache_test.go
resources/resource_factories/create/create.go
tpl/data/resources_test.go

index d3ef0199195e2a44886306649fe90f0e2196974a..1b78398ccbd85242737eadaddb2bf9745f831bf8 100644 (file)
@@ -1,6 +1,7 @@
 /hugo
 docs/public*
 /.idea
+.vscode/*
 hugo.exe
 *.test
 *.prof
index 6a051a264950384b1892e939e208cb0b28acbd7a..6ac36a5c127a3bd2885fe2f2f04e9bf363e00ded 100644 (file)
@@ -276,7 +276,7 @@ func TestFileCacheReadOrCreateErrorInRead(t *testing.T) {
                }
        }
 
-       cache := NewCache(afero.NewMemMapFs(), 100*time.Hour, "")
+       cache := NewCache(afero.NewMemMapFs(), 100*time.Hour, 0, "")
 
        const id = "a32"
 
index 8edf501f785640d92138843679681d4388ac3ab9..9b8d73ad478bbdf618e1e3a8b356d1c1adb9c021 100644 (file)
@@ -18,6 +18,7 @@ package create
 import (
        "bufio"
        "bytes"
+       "context"
        "fmt"
        "io"
        "io/ioutil"
@@ -180,6 +181,14 @@ func (c *Client) FromRemote(uri string, options map[string]interface{}) (resourc
                        }
                        addUserProvidedHeaders(headers, req)
                }
+
+               // Workaround for https://github.com/golang/go/issues/49366
+               // This is the entire lifetime of the request.
+               ctx, cancel := context.WithTimeout(req.Context(), 30*time.Second)
+               defer cancel()
+
+               req = req.WithContext(ctx)
+
                res, err := c.httpClient.Do(req)
                if err != nil {
                        return nil, err
index 1bf6d769fa4126e0bedcc73bf52b9bc55f4e0c54..a1ce9ecb657a99ae07cc153657a7213ca3421e0f 100644 (file)
@@ -34,7 +34,6 @@ import (
        "github.com/gohugoio/hugo/hugofs"
        "github.com/gohugoio/hugo/langs"
        "github.com/spf13/afero"
-       
 )
 
 func TestScpGetLocal(t *testing.T) {
@@ -87,7 +86,7 @@ func TestScpGetRemote(t *testing.T) {
        t.Parallel()
        c := qt.New(t)
        fs := new(afero.MemMapFs)
-       cache := filecache.NewCache(fs, 100, "")
+       cache := filecache.NewCache(fs, 100, 0, "")
 
        tests := []struct {
                path    string