Skip to content

Commit 2a994c2

Browse files
committed
feat: allow dial timeout and keep alive period to be configurable
Settings are applied to every connection. Signed-off-by: Andrey Smirnov <[email protected]>
1 parent 3c8f347 commit 2a994c2

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module github.com/talos-systems/go-loadbalancer
22

33
go 1.14
44

5+
replace inet.af/tcpproxy => github.com/smira/tcpproxy v0.0.0-20201014215138-25207f0e2105
6+
57
require (
68
github.com/davecgh/go-spew v1.1.1 // indirect
79
github.com/kr/pretty v0.1.0 // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,15 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
88
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
99
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1010
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
11+
github.com/smira/tcpproxy v0.0.0-20201014215138-25207f0e2105 h1:R88m/hWIpn4uM+Jx6XgV+aACDpOwOR4MUciaU1GnaYM=
12+
github.com/smira/tcpproxy v0.0.0-20201014215138-25207f0e2105/go.mod h1:yDIWrelwlTRXdKvQqqQ+8lCwCjbSRtkat49REnui7hk=
1113
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1214
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1315
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
14-
github.com/talos-systems/go-retry v0.0.0-20200902131929-073067bd95a7 h1:KL8Nz3tlwA1UhTsoGQXwg1gDoYPldiwfTtcipcuALXM=
15-
github.com/talos-systems/go-retry v0.0.0-20200902131929-073067bd95a7/go.mod h1:HiXQqyVStZ35uSY/MTLWVvQVmC3lIW2MS5VdDaMtoKM=
1616
github.com/talos-systems/go-retry v0.1.0 h1:O+OeZR54CQ1+ch99p/81Pqi5GqJH6LIu1MTN/N0vd78=
1717
github.com/talos-systems/go-retry v0.1.0/go.mod h1:HiXQqyVStZ35uSY/MTLWVvQVmC3lIW2MS5VdDaMtoKM=
1818
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1919
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
2020
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2121
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
2222
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
23-
inet.af/tcpproxy v0.0.0-20200125044825-b6bb9b5b8252 h1:gmJCKidOfjKDUHF1jjke+I+2iQIyE3HNNxu2OKO/FUI=
24-
inet.af/tcpproxy v0.0.0-20200125044825-b6bb9b5b8252/go.mod h1:zq+R+tLcdHugi7Jt+FtIQY6m6wtX34lr2CdQVH2fhW0=

loadbalancer/loadbalancer.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"log"
1212
"net"
13+
"time"
1314

1415
"inet.af/tcpproxy"
1516

@@ -27,6 +28,9 @@ import (
2728
type TCP struct {
2829
tcpproxy.Proxy
2930

31+
DialTimeout time.Duration
32+
KeepAlivePeriod time.Duration
33+
3034
Logger *log.Logger
3135

3236
routes map[string]*upstream.List
@@ -51,8 +55,10 @@ func (upstream lbUpstream) HealthCheck(ctx context.Context) error {
5155
}
5256

5357
type lbTarget struct {
54-
list *upstream.List
55-
logger *log.Logger
58+
list *upstream.List
59+
logger *log.Logger
60+
dialTimeout time.Duration
61+
keepAlivePeriod time.Duration
5662
}
5763

5864
func (target *lbTarget) HandleConn(conn net.Conn) {
@@ -69,6 +75,8 @@ func (target *lbTarget) HandleConn(conn net.Conn) {
6975
target.logger.Printf("proxying connection %s -> %s", conn.RemoteAddr(), upstream.upstream)
7076

7177
upstreamTarget := tcpproxy.To(upstream.upstream)
78+
upstreamTarget.DialTimeout = target.dialTimeout
79+
upstreamTarget.KeepAlivePeriod = target.keepAlivePeriod
7280
upstreamTarget.OnDialError = func(src net.Conn, dstDialErr error) {
7381
src.Close() //nolint: errcheck
7482

@@ -78,6 +86,8 @@ func (target *lbTarget) HandleConn(conn net.Conn) {
7886
}
7987

8088
upstreamTarget.HandleConn(conn)
89+
90+
target.logger.Printf("closing connection %s -> %s", conn.RemoteAddr(), upstream.upstream)
8191
}
8292

8393
// AddRoute installs load balancer route from listen address ipAddr to list of upstreams.
@@ -109,8 +119,10 @@ func (t *TCP) AddRoute(ipPort string, upstreamAddrs []string, options ...upstrea
109119
t.routes[ipPort] = list
110120

111121
t.Proxy.AddRoute(ipPort, &lbTarget{
112-
list: list,
113-
logger: t.Logger,
122+
list: list,
123+
logger: t.Logger,
124+
dialTimeout: t.DialTimeout,
125+
keepAlivePeriod: t.KeepAlivePeriod,
114126
})
115127

116128
return nil

0 commit comments

Comments
 (0)