2019-05-22 21:23:23 +00:00
|
|
|
package netutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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(name, addr string) *TCPDialer {
|
|
|
|
d := &TCPDialer{
|
|
|
|
d: &net.Dialer{
|
|
|
|
Timeout: time.Second,
|
|
|
|
KeepAlive: time.Second,
|
|
|
|
},
|
|
|
|
|
|
|
|
addr: addr,
|
|
|
|
|
|
|
|
dials: metrics.NewCounter(fmt.Sprintf(`vm_tcpdialer_dials_total{name=%q, addr=%q}`, name, addr)),
|
|
|
|
dialErrors: metrics.NewCounter(fmt.Sprintf(`vm_tcpdialer_errors_total{name=%q, addr=%q, type="dial"}`, name, addr)),
|
|
|
|
}
|
|
|
|
d.connMetrics.init("vm_tcpdialer", name, addr)
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// TCPDialer is used for dialing the addr passed to NewTCPDialer.
|
|
|
|
//
|
|
|
|
// It also gathers various stats for dialed connections.
|
|
|
|
type TCPDialer struct {
|
|
|
|
d *net.Dialer
|
|
|
|
|
|
|
|
addr string
|
|
|
|
|
|
|
|
dials *metrics.Counter
|
|
|
|
dialErrors *metrics.Counter
|
|
|
|
|
|
|
|
connMetrics
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dial dials the addr passed to NewTCPDialer.
|
|
|
|
func (d *TCPDialer) Dial() (net.Conn, error) {
|
|
|
|
d.dials.Inc()
|
2021-03-16 22:16:06 +00:00
|
|
|
network := GetTCPNetwork()
|
2019-12-01 00:52:22 +00:00
|
|
|
c, err := d.d.Dial(network, d.addr)
|
2019-05-22 21:23:23 +00:00
|
|
|
if err != nil {
|
|
|
|
d.dialErrors.Inc()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
d.conns.Inc()
|
|
|
|
sc := &statConn{
|
|
|
|
Conn: c,
|
|
|
|
cm: &d.connMetrics,
|
|
|
|
}
|
|
|
|
return sc, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addr returns the address the dialer dials to.
|
|
|
|
func (d *TCPDialer) Addr() string {
|
|
|
|
return d.addr
|
|
|
|
}
|