Skip to content

Commit 09f7dc1

Browse files
committed
Add HTTP connection timeout
As per https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f 779#.8j7att7r0
1 parent 78692ce commit 09f7dc1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

main.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func main() {
4848
os.Exit(0)
4949
}
5050

51+
var httpClient = &http.Client{
52+
Timeout: time.Second * 10,
53+
}
54+
5155
httpheader := "http://"
5256

5357
if *useSsl != "" {
@@ -70,7 +74,7 @@ func main() {
7074
fmt.Println("Resetting the board")
7175
}
7276

73-
resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
77+
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*syncEndpoint, "", nil)
7478
if err != nil || resp.StatusCode != syncRetCode {
7579
if *verbose {
7680
fmt.Println("Failed to reset the board, upload failed")
@@ -88,7 +92,7 @@ func main() {
8892
timeout := 0
8993

9094
for timeout < 10 {
91-
resp, err := http.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
95+
resp, err := httpClient.Get(httpheader + *networkAddress + ":" + *networkPort + *syncEndpoint)
9296
if err != nil {
9397
if *verbose {
9498
fmt.Println("Failed to reset the board, upload failed")
@@ -152,7 +156,7 @@ func main() {
152156
req.SetBasicAuth(*username, *password)
153157
}
154158

155-
resp, err := http.DefaultClient.Do(req)
159+
resp, err := httpClient.Do(req)
156160
if err != nil {
157161
if *verbose {
158162
fmt.Println("Error flashing the sketch")
@@ -181,7 +185,7 @@ func main() {
181185
fmt.Println("Resetting the board")
182186
}
183187

184-
resp, err := http.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
188+
resp, err := httpClient.Post(httpheader+*networkAddress+":"+*networkPort+*resetEndpoint, "", nil)
185189
if err != nil {
186190
if *verbose {
187191
fmt.Println("Failed to reset the board, please reset maually")

0 commit comments

Comments
 (0)