Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(client): add timeout to fetchResource to prevent hangs #2322

Merged
merged 1 commit into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion client/go/outline/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,22 @@ package outline
import (
"io"
"net/http"
"time"

"github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors"
)

const fetchTimeout = 10 * time.Second
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the TS code would specify this, but this is a good mitigation.

Copy link
Contributor Author

@jyyi1 jyyi1 Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, TS code should combine fetchResource and the next establishVPN into a single "transaction", so both operations can be cancelled by a single context object (for example, when a user wants to connect to another server, we should cancel all existing fetchResource and establishVPN operations).


// fetchResource fetches a resource from the given URL.
//
// The function makes an HTTP GET request to the specified URL and returns the response body as a
// string. If the request fails or the server returns a non-2xx status code, an error is returned.
func fetchResource(url string) (string, error) {
resp, err := http.Get(url)
client := &http.Client{
Timeout: fetchTimeout,
}
resp, err := client.Get(url)
if err != nil {
return "", platerrors.PlatformError{
Code: platerrors.FetchConfigFailed,
Expand Down
27 changes: 27 additions & 0 deletions client/go/outline/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/Jigsaw-Code/outline-apps/client/go/outline/platerrors"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -100,3 +101,29 @@ func TestFetchResource_BodyReadError(t *testing.T) {
require.Equal(t, platerrors.FetchConfigFailed, perr.Code)
require.Error(t, perr.Cause)
}

func TestFetchResource_Timeout(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

const (
MaxFetchWaitTime = 12 * time.Second
ServerDelay = 20 * time.Second
)

testDone := make(chan bool)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
select {
case <-time.After(ServerDelay):
w.WriteHeader(http.StatusNoContent)
case <-testDone:
}
}))
defer server.Close()

start := time.Now()
content, err := fetchResource(server.URL)
duration := time.Since(start)
testDone <- true

require.LessOrEqual(t, duration, MaxFetchWaitTime, "fetchResource should time out in 10s")
require.Error(t, err, "fetchResource should return a non-nil timeout error")
require.Empty(t, content)
}
Loading