From 38543c2813a1075e09693894625421309d8ef333 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Fri, 17 May 2019 12:55:17 -0400 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/net/tcpsock.go | 5 +++++ src/net/tcpsockopt_darwin.go | 3 +-- src/net/tcpsockopt_dragonfly.go | 3 +-- src/net/tcpsockopt_solaris.go | 3 +-- src/net/tcpsockopt_unix.go | 3 +-- src/net/tcpsockopt_windows.go | 3 +-- 6 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/net/tcpsock.go b/src/net/tcpsock.go index 0daa2f6487..b7b73d0d81 100644 --- a/src/net/tcpsock.go +++ b/src/net/tcpsock.go @@ -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 +} diff --git a/src/net/tcpsockopt_darwin.go b/src/net/tcpsockopt_darwin.go index da0d173453..53c6756e33 100644 --- a/src/net/tcpsockopt_darwin.go +++ b/src/net/tcpsockopt_darwin.go @@ -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) } diff --git a/src/net/tcpsockopt_dragonfly.go b/src/net/tcpsockopt_dragonfly.go index 2b018f2bb2..b473c02b68 100644 --- a/src/net/tcpsockopt_dragonfly.go +++ b/src/net/tcpsockopt_dragonfly.go @@ -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) } diff --git a/src/net/tcpsockopt_solaris.go b/src/net/tcpsockopt_solaris.go index 019fe349eb..f15e589dc0 100644 --- a/src/net/tcpsockopt_solaris.go +++ b/src/net/tcpsockopt_solaris.go @@ -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) diff --git a/src/net/tcpsockopt_unix.go b/src/net/tcpsockopt_unix.go index d5892588fe..fb0ecb8dc7 100644 --- a/src/net/tcpsockopt_unix.go +++ b/src/net/tcpsockopt_unix.go @@ -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) } diff --git a/src/net/tcpsockopt_windows.go b/src/net/tcpsockopt_windows.go index 73dead11d0..4a0b09465e 100644 --- a/src/net/tcpsockopt_windows.go +++ b/src/net/tcpsockopt_windows.go @@ -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,