Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,21 @@ func newHTTPClient(u *url.URL, tlsConfig *tls.Config, timeout time.Duration) *ht
switch u.Scheme {
default:
httpTransport.Dial = func(proto, addr string) (net.Conn, error) {
return net.DialTimeout(proto, addr, timeout)
conn, err := net.DialTimeout(proto, addr, timeout)
if tcpConn, ok := conn.(*net.TCPConn); ok {
// Set TCP user timeout. Sender breaks TCP connection
// if packets are not acknowledged after 20 seconds. This is a
// relatively new TCP option to improve dead peer detection.
// Do not fail newHTTPClient if OS doesn's support it.

// user timeout shouldn't be too aggressive
userTimeout := timeout
if userTimeout < 20*time.Second {
userTimeout = 20 * time.Second
}
SetTCPUserTimeout(tcpConn, userTimeout)
}
return conn, err
}
case "unix":
socketPath := u.Path
Expand Down
25 changes: 25 additions & 0 deletions utils_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// +build !windows

package dockerclient

// #include <netinet/tcp.h>
import "C"

import (
"net"
"os"
"syscall"
"time"
)

// SetTCPUserTimeout sets TCP_USER_TIMEOUT according to RFC5842
func SetTCPUserTimeout(conn *net.TCPConn, uto time.Duration) error {
f, err := conn.File()
if err != nil {
return err
}
defer f.Close()

msecs := int(uto.Nanoseconds() / 1e6)
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(f.Fd()), syscall.SOL_TCP, C.TCP_USER_TIMEOUT, msecs))
}
28 changes: 28 additions & 0 deletions utils_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build windows

package dockerclient

// #include <Ws2tcpip.h>
import "C"

import (
"net"
"os"
"syscall"
"time"
)

// SetTCPUserTimeout sets TCP_MAXRT in Windows
func SetTCPUserTimeout(conn *net.TCPConn, uto time.Duration) error {
f, err := conn.File()
if err != nil {
return err
}
defer f.Close()

// TCP_MAXRT in Windows is set as seconds
secs := int(uto.Nanoseconds() / 1e9)

// from MSDN, TCP_MAXRT is supported since Windows Vista
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(f.Fd()), syscall.SOL_TCP, C.TCP_MAXRT, secs))
}