Skip to content

Commit

Permalink
resources: Add timeout to the HTTP request in Get
Browse files Browse the repository at this point in the history
Workaround for golang/go#49366
  • Loading branch information
bep committed Dec 2, 2021
1 parent 94f149b commit 93572e5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,6 +1,7 @@
/hugo
docs/public*
/.idea
.vscode/*
hugo.exe
*.test
*.prof
Expand Down
2 changes: 1 addition & 1 deletion cache/filecache/filecache_test.go
Expand Up @@ -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"

Expand Down
9 changes: 9 additions & 0 deletions resources/resource_factories/create/create.go
Expand Up @@ -18,6 +18,7 @@ package create
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions tpl/data/resources_test.go
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/gohugoio/hugo/hugofs"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"

)

func TestScpGetLocal(t *testing.T) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 93572e5

Please sign in to comment.