You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gserver/dial.go

76 lines
1.2 KiB
Go

8 years ago
package main
import (
"context"
"net"
"time"
)
var dialer *net.Dialer
func dial(network, addr string) (net.Conn, error) {
var err error
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return dialer.Dial(network, addr)
}
ips, err := lookupHost(host)
if err != nil {
return nil, err
}
var conn net.Conn
for _, ip := range ips {
address := net.JoinHostPort(ip, port)
if conn, err = dialer.Dial(network, address); err == nil {
return conn, err
}
}
// return last error
return conn, err
}
func dialContext(ctx context.Context, network, addr string) (net.Conn, error) {
var err error
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if ip := net.ParseIP(host); ip != nil {
return dialer.DialContext(ctx, network, addr)
}
ips, err := lookupHost(host)
if err != nil {
return nil, err
}
var conn net.Conn
for _, ip := range ips {
address := net.JoinHostPort(ip, port)
if conn, err = dialer.DialContext(ctx, network, address); err == nil {
return conn, err
}
}
// return last error
return conn, err
}
func init() {
dialer = &net.Dialer{
Timeout: 2 * time.Second,
}
}