Skip to content

Commit bc264bc

Browse files
specify timeout to avoid crash
1 parent f96d518 commit bc264bc

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
todo.txt

app/app.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io/ioutil"
77
"net/http"
88
"os"
9+
"time"
910
)
1011

1112
type Recipe struct {
@@ -23,7 +24,15 @@ type Recipe struct {
2324
}
2425

2526
func main() {
26-
resp, err := http.Get("https://s3-eu-west-1.amazonaws.com/test-golang-recipes/1")
27+
// Specify timeout to avoid apps to hang unexpecedly since there is no timeout by default
28+
// https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779
29+
httpClient := &http.Client{
30+
Timeout: time.Second * 2,
31+
}
32+
req, err := http.NewRequest("GET", "https://s3-eu-west-1.amazonaws.com/test-golang-recipes/1", nil)
33+
check(err)
34+
req.Header.Set("User-Agent", "hellofresh")
35+
resp, err := httpClient.Do(req)
2736
check(err)
2837
body, err := ioutil.ReadAll(resp.Body)
2938
check(err)

0 commit comments

Comments
 (0)