]> Cypherpunks repositories - gostls13.git/commitdiff
net: add SetKeepAlivePeriod
authorDavid Presotto <presotto@gmail.com>
Mon, 15 Jul 2013 22:40:55 +0000 (18:40 -0400)
committerRuss Cox <rsc@golang.org>
Mon, 15 Jul 2013 22:40:55 +0000 (18:40 -0400)
Sets both the duration from the last data packet to the first
keep alive packet and the duration between keep alive packets to be
the passed duration.

I've tested the function on both Darwin (10.8.4) and 4.2 Linux.
I've compiled (make.bash) for all the OS's and tested (all.bash)
on Darwin and Linux.

R=golang-dev, dave, rsc, dvyukov, presotto+facebook, nick
CC=golang-dev, veyron-team
https://golang.org/cl/11130044

src/pkg/net/tcpsock_posix.go
src/pkg/net/tcpsockopt_darwin.go [new file with mode: 0644]
src/pkg/net/tcpsockopt_openbsd.go [new file with mode: 0644]
src/pkg/net/tcpsockopt_posix.go [new file with mode: 0644]
src/pkg/net/tcpsockopt_windows.go [new file with mode: 0644]

index 876edb101cae815c0d63644d5a81e64d4f1b528d..befe6b631522586255c90a002b0982f46b63c495 100644 (file)
@@ -121,6 +121,14 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) error {
        return setKeepAlive(c.fd, keepalive)
 }
 
+// SetKeepAliveIdlePeriod sets period between keep alives.
+func (c *TCPConn) SetKeepAlivePeriod(d time.Duration) error {
+       if !c.ok() {
+               return syscall.EINVAL
+       }
+       return setKeepAlivePeriod(c.fd, d)
+}
+
 // SetNoDelay controls whether the operating system should delay
 // packet transmission in hopes of sending fewer packets (Nagle's
 // algorithm).  The default is true (no delay), meaning that data is
diff --git a/src/pkg/net/tcpsockopt_darwin.go b/src/pkg/net/tcpsockopt_darwin.go
new file mode 100644 (file)
index 0000000..d052a14
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2009 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TCP socket options for darwin
+
+package net
+
+import (
+       "os"
+       "syscall"
+       "time"
+)
+
+// Set keep alive period.
+func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
+       if err := fd.incref(false); err != nil {
+               return err
+       }
+       defer fd.decref()
+
+       // The kernel expects seconds so round to next highest second.
+       d += (time.Second - time.Nanosecond)
+       secs := int(d.Seconds())
+
+       return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs))
+}
diff --git a/src/pkg/net/tcpsockopt_openbsd.go b/src/pkg/net/tcpsockopt_openbsd.go
new file mode 100644 (file)
index 0000000..25a826f
--- /dev/null
@@ -0,0 +1,27 @@
+// Copyright 2009 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TCP socket options for openbsd
+
+package net
+
+import (
+       "os"
+       "syscall"
+       "time"
+)
+
+// Set keep alive period.
+func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
+       if err := fd.incref(false); err != nil {
+               return err
+       }
+       defer fd.decref()
+
+       // The kernel expects seconds so round to next highest second.
+       d += (time.Second - time.Nanosecond)
+       secs := int(d.Seconds())
+
+       return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs))
+}
diff --git a/src/pkg/net/tcpsockopt_posix.go b/src/pkg/net/tcpsockopt_posix.go
new file mode 100644 (file)
index 0000000..dfc0452
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2009 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build freebsd linux netbsd
+
+package net
+
+import (
+       "os"
+       "syscall"
+       "time"
+)
+
+// Set keep alive period.
+func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
+       if err := fd.incref(false); err != nil {
+               return err
+       }
+       defer fd.decref()
+
+       // The kernel expects seconds so round to next highest second.
+       d += (time.Second - time.Nanosecond)
+       secs := int(d.Seconds())
+
+       err := os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, secs))
+       if err != nil {
+               return err
+       }
+       return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, secs))
+}
diff --git a/src/pkg/net/tcpsockopt_windows.go b/src/pkg/net/tcpsockopt_windows.go
new file mode 100644 (file)
index 0000000..538366d
--- /dev/null
@@ -0,0 +1,21 @@
+// Copyright 2009 The Go Authors.  All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// TCP socket options for windows
+
+package net
+
+import (
+       "time"
+)
+
+func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
+       if err := fd.incref(false); err != nil {
+               return err
+       }
+       defer fd.decref()
+
+       // We can't actually set this per connection.  Act as a noop rather than an error.
+       return nil
+}