Use dialer in relay checks (fixes #2732)

This commit is contained in:
Audrius Butkevicius 2016-01-30 01:52:32 +00:00
parent 016f799983
commit f59e1ad854
5 changed files with 24 additions and 30 deletions

View File

@ -14,7 +14,6 @@ import (
"github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/dialer"
"github.com/syncthing/syncthing/lib/model" "github.com/syncthing/syncthing/lib/model"
"github.com/syncthing/syncthing/lib/osutil"
) )
func init() { func init() {
@ -72,7 +71,7 @@ func tcpListener(uri *url.URL, tlsCfg *tls.Config, conns chan<- model.Intermedia
l.Debugln("connect from", conn.RemoteAddr()) l.Debugln("connect from", conn.RemoteAddr())
err = osutil.SetTCPOptions(conn.(*net.TCPConn)) err = dialer.SetTCPOptions(conn.(*net.TCPConn))
if err != nil { if err != nil {
l.Infoln(err) l.Infoln(err)
} }

View File

@ -17,7 +17,6 @@ import (
"golang.org/x/net/proxy" "golang.org/x/net/proxy"
"github.com/syncthing/syncthing/lib/logger" "github.com/syncthing/syncthing/lib/logger"
"github.com/syncthing/syncthing/lib/osutil"
) )
var ( var (
@ -55,7 +54,7 @@ func dialWithFallback(proxyDialFunc dialFunc, fallbackDialFunc dialFunc, network
if err == nil { if err == nil {
l.Debugf("Dialing %s address %s via proxy - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr()) l.Debugf("Dialing %s address %s via proxy - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
if tcpconn, ok := conn.(*net.TCPConn); ok { if tcpconn, ok := conn.(*net.TCPConn); ok {
osutil.SetTCPOptions(tcpconn) SetTCPOptions(tcpconn)
} }
return dialerConn{ return dialerConn{
conn, newDialerAddr(network, addr), conn, newDialerAddr(network, addr),
@ -67,7 +66,7 @@ func dialWithFallback(proxyDialFunc dialFunc, fallbackDialFunc dialFunc, network
if err == nil { if err == nil {
l.Debugf("Dialing %s address %s via fallback - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr()) l.Debugf("Dialing %s address %s via fallback - success, %s -> %s", network, addr, conn.LocalAddr(), conn.RemoteAddr())
if tcpconn, ok := conn.(*net.TCPConn); ok { if tcpconn, ok := conn.(*net.TCPConn); ok {
osutil.SetTCPOptions(tcpconn) SetTCPOptions(tcpconn)
} }
} else { } else {
l.Debugf("Dialing %s address %s via fallback - error %s", network, addr, err) l.Debugf("Dialing %s address %s via fallback - error %s", network, addr, err)

View File

@ -46,3 +46,21 @@ func DialTimeout(network, addr string, timeout time.Duration) (net.Conn, error)
} }
return net.DialTimeout(network, addr, timeout) return net.DialTimeout(network, addr, timeout)
} }
// SetTCPOptions sets syncthings default TCP options on a TCP connection
func SetTCPOptions(conn *net.TCPConn) error {
var err error
if err = conn.SetLinger(0); err != nil {
return err
}
if err = conn.SetNoDelay(false); err != nil {
return err
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
return err
}
if err = conn.SetKeepAlive(true); err != nil {
return err
}
return nil
}

View File

@ -11,12 +11,10 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings" "strings"
"time"
"github.com/calmh/du" "github.com/calmh/du"
"github.com/syncthing/syncthing/lib/sync" "github.com/syncthing/syncthing/lib/sync"
@ -223,21 +221,3 @@ func DiskFreePercentage(path string) (freePct float64, err error) {
u, err := du.Get(path) u, err := du.Get(path)
return (float64(u.FreeBytes) / float64(u.TotalBytes)) * 100, err return (float64(u.FreeBytes) / float64(u.TotalBytes)) * 100, err
} }
// SetTCPOptions sets syncthings default TCP options on a TCP connection
func SetTCPOptions(conn *net.TCPConn) error {
var err error
if err = conn.SetLinger(0); err != nil {
return err
}
if err = conn.SetNoDelay(false); err != nil {
return err
}
if err = conn.SetKeepAlivePeriod(60 * time.Second); err != nil {
return err
}
if err = conn.SetKeepAlive(true); err != nil {
return err
}
return nil
}

View File

@ -7,20 +7,18 @@
package osutil package osutil
import ( import (
"net"
"net/url" "net/url"
"time" "time"
"github.com/syncthing/syncthing/lib/dialer"
) )
// TCPPing returns the duration required to establish a TCP connection // TCPPing returns the duration required to establish a TCP connection
// to the given host. ICMP packets require root priviledges, hence why we use // to the given host. ICMP packets require root priviledges, hence why we use
// tcp. // tcp.
func TCPPing(address string) (time.Duration, error) { func TCPPing(address string) (time.Duration, error) {
dialer := net.Dialer{
Deadline: time.Now().Add(time.Second),
}
start := time.Now() start := time.Now()
conn, err := dialer.Dial("tcp", address) conn, err := dialer.DialTimeout("tcp", address, time.Second)
if conn != nil { if conn != nil {
conn.Close() conn.Close()
} }