mirror of
https://github.com/golang/go
synced 2024-11-19 21:14:43 -07:00
net: fix inconsistent behavior across platforms in SetKeepAlivePeriod
The previous implementation used per-socket TCP keepalive options wrong. For example, it used another level socket option to control TCP and it didn't use TCP_KEEPINTVL option when possible. Fixes #8683. Fixes #8701. Update #8679 LGTM=iant R=golang-codereviews, iant CC=golang-codereviews https://golang.org/cl/136480043
This commit is contained in:
parent
689dc60c14
commit
f956740163
@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// TCP socket options for darwin
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
@ -12,16 +10,20 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Set keep alive period.
|
||||
const sysTCP_KEEPINTVL = 0x101
|
||||
|
||||
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
if err := fd.incref(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.decref()
|
||||
|
||||
// The kernel expects seconds so round to next highest second.
|
||||
d += (time.Second - time.Nanosecond)
|
||||
secs := int(d.Seconds())
|
||||
|
||||
switch err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err {
|
||||
case nil, syscall.ENOPROTOOPT: // OS X 10.7 and earlier don't support this option
|
||||
default:
|
||||
return os.NewSyscallError("setsockopt", err)
|
||||
}
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs))
|
||||
}
|
||||
|
@ -10,20 +10,17 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Set keep alive period.
|
||||
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
if err := fd.incref(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.decref()
|
||||
|
||||
// The kernel expects milliseconds so round to next highest millisecond.
|
||||
// The kernel expects milliseconds so round to next highest
|
||||
// millisecond.
|
||||
d += (time.Millisecond - time.Nanosecond)
|
||||
msecs := int(time.Duration(d.Nanoseconds()) / time.Millisecond)
|
||||
|
||||
err := os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, msecs))
|
||||
if err != nil {
|
||||
return err
|
||||
msecs := int(d / time.Millisecond)
|
||||
if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, msecs); err != nil {
|
||||
return os.NewSyscallError("setsockopt", err)
|
||||
}
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, msecs))
|
||||
}
|
||||
|
@ -1,27 +0,0 @@
|
||||
// Copyright 2013 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// TCP socket options for solaris
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
"os"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Set keep alive period.
|
||||
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
if err := fd.incref(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.decref()
|
||||
|
||||
// The kernel expects seconds so round to next highest second.
|
||||
d += (time.Second - time.Nanosecond)
|
||||
secs := int(d.Seconds())
|
||||
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.SO_KEEPALIVE, secs))
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build nacl openbsd
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
@ -10,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
// OpenBSD has no user-settable per-socket TCP keepalive
|
||||
// options.
|
||||
return syscall.EPROTONOSUPPORT
|
||||
// NaCl and OpenBSD have no user-settable per-socket TCP
|
||||
// keepalive options.
|
||||
return syscall.ENOPROTOOPT
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build freebsd linux nacl netbsd
|
||||
// +build freebsd linux netbsd solaris
|
||||
|
||||
package net
|
||||
|
||||
@ -12,20 +12,16 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Set keep alive period.
|
||||
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
if err := fd.incref(); err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.decref()
|
||||
|
||||
// The kernel expects seconds so round to next highest second.
|
||||
d += (time.Second - time.Nanosecond)
|
||||
secs := int(d.Seconds())
|
||||
|
||||
err := os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs))
|
||||
if err != nil {
|
||||
return err
|
||||
if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil {
|
||||
return os.NewSyscallError("setsockopt", err)
|
||||
}
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, secs))
|
||||
}
|
||||
|
@ -2,8 +2,6 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// TCP socket options for windows
|
||||
|
||||
package net
|
||||
|
||||
import (
|
||||
@ -18,14 +16,14 @@ func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
|
||||
return err
|
||||
}
|
||||
defer fd.decref()
|
||||
|
||||
// Windows expects milliseconds so round to next highest millisecond.
|
||||
// The kernel expects milliseconds so round to next highest
|
||||
// millisecond.
|
||||
d += (time.Millisecond - time.Nanosecond)
|
||||
millis := uint32(d / time.Millisecond)
|
||||
msecs := uint32(d / time.Millisecond)
|
||||
ka := syscall.TCPKeepalive{
|
||||
OnOff: 1,
|
||||
Time: millis,
|
||||
Interval: millis,
|
||||
Time: msecs,
|
||||
Interval: msecs,
|
||||
}
|
||||
ret := uint32(0)
|
||||
size := uint32(unsafe.Sizeof(ka))
|
||||
|
Loading…
Reference in New Issue
Block a user