Skip to content

Commit 38543c2

Browse files
tamirdmvdan
authored andcommitted
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 <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent c3c5366 commit 38543c2

6 files changed

+10
-10
lines changed

src/net/tcpsock.go

+5
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,8 @@ func ListenTCP(network string, laddr *TCPAddr) (*TCPListener, error) {
337337
}
338338
return ln, nil
339339
}
340+
341+
// roundDurationUp rounds d to the next multiple of to.
342+
func roundDurationUp(d time.Duration, to time.Duration) time.Duration {
343+
return (d + to - 1) / to
344+
}

src/net/tcpsockopt_darwin.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ const sysTCP_KEEPINTVL = 0x101
1515

1616
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
1717
// The kernel expects seconds so round to next highest second.
18-
d += (time.Second - time.Nanosecond)
19-
secs := int(d.Seconds())
18+
secs := int(roundDurationUp(d, time.Second))
2019
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err != nil {
2120
return wrapSyscallError("setsockopt", err)
2221
}

src/net/tcpsockopt_dragonfly.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
1414
// The kernel expects milliseconds so round to next highest
1515
// millisecond.
16-
d += (time.Millisecond - time.Nanosecond)
17-
msecs := int(d / time.Millisecond)
16+
msecs := int(roundDurationUp(d, time.Millisecond))
1817
if err := fd.pfd.SetsockoptInt(syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, msecs); err != nil {
1918
return wrapSyscallError("setsockopt", err)
2019
}

src/net/tcpsockopt_solaris.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ import (
1313
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
1414
// The kernel expects milliseconds so round to next highest
1515
// millisecond.
16-
d += (time.Millisecond - time.Nanosecond)
17-
msecs := int(d / time.Millisecond)
16+
msecs := int(roundDurationUp(d, time.Millisecond))
1817

1918
// Normally we'd do
2019
// syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs)

src/net/tcpsockopt_unix.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import (
1414

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

src/net/tcpsockopt_windows.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
1616
// The kernel expects milliseconds so round to next highest
1717
// millisecond.
18-
d += (time.Millisecond - time.Nanosecond)
19-
msecs := uint32(d / time.Millisecond)
18+
msecs := uint32(roundDurationUp(d, time.Millisecond))
2019
ka := syscall.TCPKeepalive{
2120
OnOff: 1,
2221
Time: msecs,

0 commit comments

Comments
 (0)