-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix to apply timeout to HTTP request
* If it have a general idea of what timeout to expect, apply it. * Applied 30 seconds for requests used in UPNP. * Applied 60 seconds for requests used in testing. * Do nothing for use cases where callers are using with and without timeouts.
- Loading branch information
Showing
7 changed files
with
92 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package net | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
// HttpGet sends a GET request to the specified url with timeout and return the response. | ||
func HttpGet(url string, timeout time.Duration) (*http.Response, error) { | ||
request, err := http.NewRequestWithContext(context.Background(), "GET", url, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return HttpRequest(request, timeout) | ||
} | ||
|
||
// HttpRequest sends the specified HTTP requests with timeout and return the response. | ||
// For stability and security reason, we need to be available request timeouts, so http.Client{} and http.Get() are | ||
// overridden with functions that rely on this function. | ||
func HttpRequest(request *http.Request, timeout time.Duration) (*http.Response, error) { | ||
client := &http.Client{ | ||
Timeout: timeout, | ||
} | ||
return client.Do(request) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net | ||
|
||
import ( | ||
"github.com/stretchr/testify/require" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestHttpGet(t *testing.T) { | ||
expected := "hello, world" | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(200) | ||
w.Header().Set("Pragma", "no-cache") | ||
w.Header().Set("Cache-Control", "no-cache") | ||
w.Header().Set("Content-Type", "text/plain; charset=UTF-8") | ||
_, err := w.Write([]byte(expected)) | ||
require.NoError(t, err) | ||
})) | ||
defer server.Close() | ||
|
||
response, err := HttpGet(server.URL, 60*time.Second) | ||
require.NoError(t, err) | ||
bytes, err := io.ReadAll(response.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, expected, string(bytes)) | ||
} | ||
|
||
func TestHttpGetWithTimeout(t *testing.T) { | ||
var mtx sync.Mutex | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
mtx.Lock() | ||
mtx.Unlock() | ||
})) | ||
defer server.Close() | ||
|
||
accuracy := 0.05 | ||
timeout := 10 * time.Second | ||
mtx.Lock() | ||
defer mtx.Unlock() | ||
t0 := time.Now() | ||
_, err := HttpGet(server.URL, timeout) | ||
t1 := time.Now() | ||
require.Error(t, err) | ||
delta := t1.Sub(t0).Seconds() | ||
require.Greater(t, delta, timeout.Seconds()) | ||
require.InDeltaf(t, timeout.Seconds(), delta, accuracy, | ||
"response time of %.3f sec exceeded +%d%% of the expected timeout of %.3f sec", delta, uint(accuracy*100), timeout.Seconds()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters