lib/netutil/tcpdialer.go: reduce the code difference with enterprise branch

This commit is contained in:
Aliaksandr Valialkin 2023-08-29 12:45:57 +02:00
parent 19d61737c1
commit 339879edd0
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1

View file

@ -9,19 +9,24 @@ import (
"github.com/VictoriaMetrics/metrics"
)
// A dialer is a means to establish a connection.
type dialer interface {
Dial(network, addr string) (c net.Conn, err error)
}
// NewTCPDialer returns new dialer for dialing the given addr.
//
// The name is used in metric tags for the returned dialer.
// The name must be unique among dialers.
func NewTCPDialer(ms *metrics.Set, name, addr string, dialTimeout, userTimeout time.Duration) *TCPDialer {
nd := &net.Dialer{
Timeout: dialTimeout,
// How frequently to send keep-alive packets over established TCP connections.
KeepAlive: time.Second,
}
d := &TCPDialer{
d: &net.Dialer{
Timeout: dialTimeout,
// How frequently to send keep-alive packets over established TCP connections.
KeepAlive: time.Second,
},
d: nd,
addr: addr,
dials: ms.NewCounter(fmt.Sprintf(`vm_tcpdialer_dials_total{name=%q, addr=%q}`, name, addr)),
@ -29,7 +34,7 @@ func NewTCPDialer(ms *metrics.Set, name, addr string, dialTimeout, userTimeout t
}
d.connMetrics.init(ms, "vm_tcpdialer", name, addr)
if userTimeout > 0 {
d.d.Control = func(network, address string, c syscall.RawConn) error {
nd.Control = func(network, address string, c syscall.RawConn) error {
var err error
controlErr := c.Control(func(fd uintptr) {
err = setTCPUserTimeout(fd, userTimeout)
@ -47,7 +52,7 @@ func NewTCPDialer(ms *metrics.Set, name, addr string, dialTimeout, userTimeout t
//
// It also gathers various stats for dialed connections.
type TCPDialer struct {
d *net.Dialer
d dialer
addr string