]> Cypherpunks repositories - gostls13.git/commitdiff
net: avoid transiting durations through floats
authorTamir Duberstein <tamird@google.com>
Fri, 17 May 2019 16:55:17 +0000 (12:55 -0400)
committerDaniel Martí <mvdan@mvdan.cc>
Tue, 17 Sep 2019 16:25:15 +0000 (16:25 +0000)
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>

src/net/tcpsock.go
src/net/tcpsockopt_darwin.go
src/net/tcpsockopt_dragonfly.go
src/net/tcpsockopt_solaris.go
src/net/tcpsockopt_unix.go
src/net/tcpsockopt_windows.go

index 0daa2f6487a51571388c560ff9182edb06db194c..b7b73d0d81701f09002b4518826d3816f685822f 100644 (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
+}
index da0d173453e0c9331d155f75afbced4932b4ebb4..53c6756e33e00b3f107d3012fc11109f88219228 100644 (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)
        }
index 2b018f2bb2b1154c38f391b64756d76e2c3f5ffc..b473c02b6867db4a5afb4fcb4c0b2d947ad64aab 100644 (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)
        }
index 019fe349eb21d44aed407ee7329d164236805a68..f15e589dc058488850533a8ff510a9901cde149b 100644 (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)
index d5892588feaabd23f9b7d427eaaf2bf11b980548..fb0ecb8dc7428fc132a979be9de9d0389405c92a 100644 (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)
        }
index 73dead11d00be6597a8b4a792fb3e0fde3aaad7d..4a0b09465eea839d19a7baa91bcf0842bd311ca2 100644 (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,