lib/netutil: use IPv6 for both listening and dialing if -enabledTCP6 is set

Updates https://github.com/VictoriaMetrics/VictoriaMetrics/issues/244
This commit is contained in:
Aliaksandr Valialkin 2019-12-01 02:52:22 +02:00
parent 1595dcd3d9
commit d456ec7589
2 changed files with 12 additions and 7 deletions

View file

@ -45,7 +45,8 @@ type TCPDialer struct {
// Dial dials the addr passed to NewTCPDialer.
func (d *TCPDialer) Dial() (net.Conn, error) {
d.dials.Inc()
c, err := d.d.Dial("tcp4", d.addr)
network := getNetwork()
c, err := d.d.Dial(network, d.addr)
if err != nil {
d.dialErrors.Inc()
return nil, err

View file

@ -9,18 +9,14 @@ import (
"github.com/VictoriaMetrics/metrics"
)
var enableTCP6 = flag.Bool("enableTCP6", false, "Whether to enable listening for IPv6 TCP ports. By default only IPv4 TCP ports are listened")
var enableTCP6 = flag.Bool("enableTCP6", false, "Whether to enable IPv6 for listening and dialing. By default only IPv4 TCP is used")
// NewTCPListener returns new TCP listener for the given addr.
//
// name is used for exported metrics. Each listener in the program must have
// distinct name.
func NewTCPListener(name, addr string) (*TCPListener, error) {
network := "tcp4"
if *enableTCP6 {
// Enable listening on both tcp4 and tcp6
network = "tcp"
}
network := getNetwork()
ln, err := net.Listen(network, addr)
if err != nil {
return nil, err
@ -35,6 +31,14 @@ func NewTCPListener(name, addr string) (*TCPListener, error) {
return tln, err
}
func getNetwork() string {
if *enableTCP6 {
// Enable both tcp4 and tcp6
return "tcp"
}
return "tcp4"
}
// TCPListener listens for the addr passed to NewTCPListener.
//
// It also gathers various stats for the accepted connections.