1
0
mirror of https://github.com/golang/go synced 2024-11-18 19:14:40 -07: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 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 { func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects seconds so round to next highest second. // The kernel expects seconds so round to next highest second.
d += (time.Second - time.Nanosecond) secs := int(roundDurationUp(d, time.Second))
secs := int(d.Seconds())
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err != nil { if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err != nil {
return wrapSyscallError("setsockopt", err) return wrapSyscallError("setsockopt", err)
} }

View File

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

View File

@ -13,8 +13,7 @@ import (
func setKeepAlivePeriod(fd *netFD, d time.Duration) error { func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects milliseconds so round to next highest // The kernel expects milliseconds so round to next highest
// millisecond. // millisecond.
d += (time.Millisecond - time.Nanosecond) msecs := int(roundDurationUp(d, time.Millisecond))
msecs := int(d / time.Millisecond)
// Normally we'd do // Normally we'd do
// syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs) // 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 { func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
// The kernel expects seconds so round to next highest second. // The kernel expects seconds so round to next highest second.
d += (time.Second - time.Nanosecond) secs := int(roundDurationUp(d, time.Second))
secs := int(d.Seconds())
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil { if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs); err != nil {
return wrapSyscallError("setsockopt", err) return wrapSyscallError("setsockopt", err)
} }

View File

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