forked from fabiolb/fabio
/
tcp_proxy.go
79 lines (65 loc) · 1.7 KB
/
tcp_proxy.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
71
72
73
74
75
76
77
78
79
package tcp
import (
"io"
"log"
"net"
"time"
"github.com/fabiolb/fabio/metrics"
"github.com/fabiolb/fabio/route"
)
// Proxy implements a generic TCP proxying handler.
type Proxy struct {
// DialTimeout sets the timeout for establishing the outbound
// connection.
DialTimeout time.Duration
// Lookup returns a target host for the given request.
// The proxy will panic if this value is nil.
Lookup func(host string) *route.Target
// Conn counts the number of connections.
Conn metrics.Counter
// ConnFail counts the failed upstream connection attempts.
ConnFail metrics.Counter
// Noroute counts the failed Lookup() calls.
Noroute metrics.Counter
}
func (p *Proxy) ServeTCP(in net.Conn) error {
defer in.Close()
if p.Conn != nil {
p.Conn.Inc(1)
}
_, port, _ := net.SplitHostPort(in.LocalAddr().String())
port = ":" + port
t := p.Lookup(port)
if t == nil {
if p.Noroute != nil {
p.Noroute.Inc(1)
}
return nil
}
addr := t.URL.Host
out, err := net.DialTimeout("tcp", addr, p.DialTimeout)
if err != nil {
log.Print("[WARN] tcp: cannot connect to upstream ", addr)
if p.ConnFail != nil {
p.ConnFail.Inc(1)
}
return err
}
defer out.Close()
errc := make(chan error, 2)
cp := func(dst io.Writer, src io.Reader, c metrics.Counter) {
errc <- copyBuffer(dst, src, c)
}
// rx measures the traffic to the upstream server (in <- out)
// tx measures the traffic from the upstream server (out <- in)
rx := metrics.DefaultRegistry.GetCounter(t.TimerName + ".rx")
tx := metrics.DefaultRegistry.GetCounter(t.TimerName + ".tx")
go cp(in, out, rx)
go cp(out, in, tx)
err = <-errc
if err != nil && err != io.EOF {
log.Print("[WARN]: tcp: ", err)
return err
}
return nil
}