1
0
mirror of https://github.com/golang/go synced 2024-09-30 22:08:32 -06:00

net: avoid transiting durations through floats

This slightly simplified the code. I stumbled upon this when support was
being added to Fuchsia (and this pattern was initially cargo-culted).

Change-Id: Ica090a118a0056c5c1b51697691bc7308f0d424a
Reviewed-on: https://go-review.googlesource.com/c/go/+/177878
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Tamir Duberstein 2019-05-17 12:55:17 -04:00 committed by Daniel Martí
parent c3c53661ba
commit 38543c2813
6 changed files with 10 additions and 10 deletions

View File

@ -337,3 +337,8 @@ func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
}
return ln, nil
}
// roundDurationUp rounds d to the next multiple of to.
func roundDurationUp(d time.Duration, to time.Duration) time.Duration {
return (d + to - 1) / to
}

View File

@ -15,8 +15,7 @@ const sysTCP_KEEPINTVL = 0x101
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects seconds so round to next highest second.
d += (time.Second - time.Nanosecond)
secs := int(d.Seconds())
secs := int(roundDurationUp(d, time.Second))
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err != nil {
return wrapSyscallError("setsockopt", err)
}

View File

@ -13,8 +13,7 @@ import (
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects milliseconds so round to next highest
// millisecond.
d += (time.Millisecond - time.Nanosecond)
msecs := int(d / time.Millisecond)
msecs := int(roundDurationUp(d, time.Millisecond))
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, msecs); err != nil {
return wrapSyscallError("setsockopt", err)
}

View File

@ -13,8 +13,7 @@ import (
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects milliseconds so round to next highest
// millisecond.
d += (time.Millisecond - time.Nanosecond)
msecs := int(d / time.Millisecond)
msecs := int(roundDurationUp(d, time.Millisecond))
// Normally we'd do
// syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs)

View File

@ -14,8 +14,7 @@ import (
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects seconds so round to next highest second.
d += (time.Second - time.Nanosecond)
secs := int(d.Seconds())
secs := int(roundDurationUp(d, time.Second))
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil {
return wrapSyscallError("setsockopt", err)
}

View File

@ -15,8 +15,7 @@ import (
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects milliseconds so round to next highest
// millisecond.
d += (time.Millisecond - time.Nanosecond)
msecs := uint32(d / time.Millisecond)
msecs := uint32(roundDurationUp(d, time.Millisecond))
ka := syscall.TCPKeepalive{
OnOff: 1,
Time: msecs,