twitch: 10 second timeout and close body on redirect error too - twitch-go - twitch.tv web application in Go
(HTM) git clone git://git.codemadness.org/twitch-go
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 9626b1467f8611871ec4b0f204006c1b9436ce88
(DIR) parent ea4a79db70c57398cf9c95e5a73144728607d414
(HTM) Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sat, 25 Jul 2015 14:08:10 +0200
twitch: 10 second timeout and close body on redirect error too
Diffstat:
M src/twitch/twitch.go | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
---
(DIR) diff --git a/src/twitch/twitch.go b/src/twitch/twitch.go
@@ -6,6 +6,7 @@ import (
"io/ioutil"
"net/http"
"net/url"
+ "time"
)
type Token struct {
@@ -98,11 +99,17 @@ type Games struct {
}
func ReadAllUrl(url string) ([]byte, error) {
- resp, err := http.Get(url)
+ client := http.Client{
+ Timeout: time.Duration(10) * time.Second,
+ }
+ req, err := http.NewRequest("GET", url, nil)
+ resp, err := client.Do(req)
+ if resp != nil {
+ defer resp.Body.Close()
+ }
if err != nil {
return nil, err
}
- defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err