-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialers.go
49 lines (43 loc) · 1.08 KB
/
dialers.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 httpclient
import (
"context"
"net"
"sync/atomic"
)
func (c *client) nextConnID() int64 {
return atomic.AddInt64(&c.currentConnID, 1)
}
func (c *client) dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
connID := c.nextConnID()
dialerThing := &net.Dialer{
Timeout: c.dialTimeout,
}
if !c.disableKeepAlive {
dialerThing.KeepAlive = c.keepAliveTimeout
}
dc := dialerThing.DialContext
c.log.Printf("Dialing conn %d to %s %s", connID, network, addr)
conn, err := dc(ctx, network, addr)
if err != nil {
c.log.Printf(
"Dialing conn %d to %s %s failed with %s",
connID, network, addr, err.Error())
return conn, err
}
onClose := func() {
c.log.Printf("Closing conn %d to %s %s", connID, network, addr)
}
return newConn(conn, onClose), err
}
// below the connection wrapper to keep track of what is happening on TCP level
func newConn(conn net.Conn, onClose func()) net.Conn {
return &connWrapper{conn, onClose}
}
type connWrapper struct {
net.Conn
onClose func()
}
func (c *connWrapper) Close() error {
c.onClose()
return c.Conn.Close()
}