@@ -27,17 +27,22 @@ import (
27
27
type TCP struct {
28
28
tcpproxy.Proxy
29
29
30
+ Logger * log.Logger
31
+
30
32
routes map [string ]* upstream.List
31
33
}
32
34
33
- type lbUpstream string
35
+ type lbUpstream struct {
36
+ upstream string
37
+ logger * log.Logger
38
+ }
34
39
35
40
func (upstream lbUpstream ) HealthCheck (ctx context.Context ) error {
36
41
d := net.Dialer {}
37
42
38
- c , err := d .DialContext (ctx , "tcp" , string ( upstream ) )
43
+ c , err := d .DialContext (ctx , "tcp" , upstream . upstream )
39
44
if err != nil {
40
- log . Printf ("healthcheck failed for %q: %s" , string ( upstream ) , err )
45
+ upstream . logger . Printf ("healthcheck failed for %q: %s" , upstream . upstream , err )
41
46
42
47
return err
43
48
}
@@ -46,27 +51,28 @@ func (upstream lbUpstream) HealthCheck(ctx context.Context) error {
46
51
}
47
52
48
53
type lbTarget struct {
49
- list * upstream.List
54
+ list * upstream.List
55
+ logger * log.Logger
50
56
}
51
57
52
58
func (target * lbTarget ) HandleConn (conn net.Conn ) {
53
59
upstreamBackend , err := target .list .Pick ()
54
60
if err != nil {
55
- log .Printf ("no upstreams available, closing connection from %s" , conn .RemoteAddr ())
61
+ target . logger .Printf ("no upstreams available, closing connection from %s" , conn .RemoteAddr ())
56
62
conn .Close () //nolint: errcheck
57
63
58
64
return
59
65
}
60
66
61
- upstreamAddr := upstreamBackend .(lbUpstream ) //nolint: errcheck
67
+ upstream := upstreamBackend .(lbUpstream ) //nolint: errcheck
62
68
63
- log . Printf ("proxying connection %s -> %s" , conn .RemoteAddr (), string ( upstreamAddr ) )
69
+ target . logger . Printf ("proxying connection %s -> %s" , conn .RemoteAddr (), upstream . upstream )
64
70
65
- upstreamTarget := tcpproxy .To (string ( upstreamAddr ) )
71
+ upstreamTarget := tcpproxy .To (upstream . upstream )
66
72
upstreamTarget .OnDialError = func (src net.Conn , dstDialErr error ) {
67
73
src .Close () //nolint: errcheck
68
74
69
- log . Printf ("error dialing upstream %s: %s" , string ( upstreamAddr ) , dstDialErr )
75
+ target . logger . Printf ("error dialing upstream %s: %s" , upstream . upstream , dstDialErr )
70
76
71
77
target .list .Down (upstreamBackend )
72
78
}
@@ -79,13 +85,20 @@ func (target *lbTarget) HandleConn(conn net.Conn) {
79
85
// TCP automatically does background health checks for the upstreams and picks only healthy
80
86
// ones. Healthcheck is simple Dial attempt.
81
87
func (t * TCP ) AddRoute (ipPort string , upstreamAddrs []string , options ... upstream.ListOption ) error {
88
+ if t .Logger == nil {
89
+ t .Logger = log .New (log .Writer (), "" , log .Flags ())
90
+ }
91
+
82
92
if t .routes == nil {
83
93
t .routes = make (map [string ]* upstream.List )
84
94
}
85
95
86
96
upstreams := make ([]upstream.Backend , len (upstreamAddrs ))
87
97
for i := range upstreams {
88
- upstreams [i ] = lbUpstream (upstreamAddrs [i ])
98
+ upstreams [i ] = lbUpstream {
99
+ upstream : upstreamAddrs [i ],
100
+ logger : t .Logger ,
101
+ }
89
102
}
90
103
91
104
list , err := upstream .NewList (upstreams , options ... )
@@ -95,7 +108,10 @@ func (t *TCP) AddRoute(ipPort string, upstreamAddrs []string, options ...upstrea
95
108
96
109
t .routes [ipPort ] = list
97
110
98
- t .Proxy .AddRoute (ipPort , & lbTarget {list : list })
111
+ t .Proxy .AddRoute (ipPort , & lbTarget {
112
+ list : list ,
113
+ logger : t .Logger ,
114
+ })
99
115
100
116
return nil
101
117
}
@@ -113,7 +129,10 @@ func (t *TCP) ReconcileRoute(ipPort string, upstreamAddrs []string) error {
113
129
114
130
upstreams := make ([]upstream.Backend , len (upstreamAddrs ))
115
131
for i := range upstreams {
116
- upstreams [i ] = lbUpstream (upstreamAddrs [i ])
132
+ upstreams [i ] = lbUpstream {
133
+ upstream : upstreamAddrs [i ],
134
+ logger : t .Logger ,
135
+ }
117
136
}
118
137
119
138
list .Reconcile (upstreams )
0 commit comments