forked from nyaruka/courier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
http.go
46 lines (39 loc) · 1.04 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package utils
import (
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
)
// MakeHTTPRequest fires the passed in http request using our shared client, returning the response and any errors
func MakeHTTPRequest(req *http.Request) (*http.Response, []byte, error) {
resp, err := GetHTTPClient().Do(req)
if err != nil {
return nil, nil, err
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
// detect a non-20* response and throw
if resp.StatusCode/100 != 2 {
return nil, nil, fmt.Errorf("got non 200 status (%d) for '%s': %s", resp.StatusCode, req.URL.String(), body[:32])
}
return resp, body, err
}
var (
transport *http.Transport
client *http.Client
once sync.Once
)
// GetHTTPClient returns the shared HTTP client used by all Courier threads
func GetHTTPClient() *http.Client {
once.Do(func() {
timeout := time.Duration(30 * time.Second)
transport = &http.Transport{
MaxIdleConns: 10,
IdleConnTimeout: 30 * time.Second,
}
client = &http.Client{Transport: transport, Timeout: timeout}
})
return client
}