forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.go
49 lines (44 loc) · 1.17 KB
/
http_client.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
package s3gof3r
import (
"net"
"net/http"
"time"
)
type deadlineConn struct {
Timeout time.Duration
net.Conn
}
func (c *deadlineConn) Read(b []byte) (n int, err error) {
if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
return
}
return c.Conn.Read(b)
}
func (c *deadlineConn) Write(b []byte) (n int, err error) {
if err = c.Conn.SetDeadline(time.Now().Add(c.Timeout)); err != nil {
return
}
return c.Conn.Write(b)
}
// ClientWithTimeout is an http client optimized for high throughput
// to S3, It times out more agressively than the default
// http client in net/http as well as setting deadlines on the TCP connection
func ClientWithTimeout(timeout time.Duration) *http.Client {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: func(netw, addr string) (net.Conn, error) {
c, err := net.DialTimeout(netw, addr, timeout)
if err != nil {
return nil, err
}
if tc, ok := c.(*net.TCPConn); ok {
tc.SetKeepAlive(true)
tc.SetKeepAlivePeriod(timeout)
}
return &deadlineConn{timeout, c}, nil
},
ResponseHeaderTimeout: timeout,
MaxIdleConnsPerHost: 10,
}
return &http.Client{Transport: transport}
}