-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
62 lines (54 loc) · 1.46 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"../../util"
"bytes"
"fmt"
"net"
"net/http"
"time"
)
var transport *http.Transport
var httpclient *http.Client
var userAgent string
func init() {
// use custom transport for deadlines
transport = &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, time.Duration(*httpTimeoutMs)*time.Millisecond)
if err != nil {
return nil, err
}
return &deadlinedConn{c}, nil
},
}
httpclient = &http.Client{Transport: transport}
userAgent = fmt.Sprintf("nsq_to_http v%s", util.BINARY_VERSION)
}
type deadlinedConn struct {
net.Conn
}
func (c *deadlinedConn) Read(b []byte) (n int, err error) {
c.Conn.SetReadDeadline(time.Now().Add(time.Duration(*httpTimeoutMs) * time.Millisecond))
return c.Conn.Read(b)
}
func (c *deadlinedConn) Write(b []byte) (n int, err error) {
c.Conn.SetWriteDeadline(time.Now().Add(time.Duration(*httpTimeoutMs) * time.Millisecond))
return c.Conn.Write(b)
}
func HttpGet(endpoint string) (*http.Response, error) {
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
return httpclient.Do(req)
}
func HttpPost(endpoint string, body *bytes.Buffer) (*http.Response, error) {
req, err := http.NewRequest("POST", endpoint, body)
if err != nil {
return nil, err
}
req.Header.Set("User-Agent", userAgent)
req.Header.Set("Content-Type", "application/octet-stream")
return httpclient.Do(req)
}