-
Notifications
You must be signed in to change notification settings - Fork 61
/
conn.go
70 lines (60 loc) · 1.35 KB
/
conn.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
63
64
65
66
67
68
69
70
package http
import (
"io"
"net"
"sync"
"time"
"github.com/gorilla/http/client"
)
// Dialer can dial a remote HTTP server.
type Dialer interface {
// Dial dials a remote http server returning a Conn.
Dial(network, addr string) (Conn, error)
}
type dialer struct {
sync.Mutex // protects following fields
conns map[string][]Conn // maps addr to a, possibly empty, slice of existing Conns
}
func (d *dialer) Dial(network, addr string) (Conn, error) {
d.Lock()
if d.conns == nil {
d.conns = make(map[string][]Conn)
}
if c, ok := d.conns[addr]; ok {
if len(c) > 0 {
conn := c[0]
c[0], c = c[len(c)-1], c[:len(c)-1] // nolint:staticcheck
d.Unlock()
return conn, nil
}
}
d.Unlock()
c, err := net.Dial(network, addr)
return &conn{
Client: client.NewClient(c),
Conn: c,
dialer: d,
}, err
}
// Conn represnts a connection which can be used to communicate
// with a remote HTTP server.
type Conn interface {
client.Client
io.Closer
SetDeadline(time.Time) error
SetReadDeadline(time.Time) error
SetWriteDeadline(time.Time) error
// Release returns the Conn to the Dialer for reuse.
Release()
}
type conn struct {
client.Client
net.Conn
*dialer
}
func (c *conn) Release() {
c.dialer.Lock()
defer c.dialer.Unlock()
addr := c.Conn.RemoteAddr().String()
c.dialer.conns[addr] = append(c.dialer.conns[addr], c)
}