Commit 918922cf authored by David Presotto's avatar David Presotto Committed by Russ Cox

net: add SetKeepAlivePeriod

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
parent 4e3051c6
...@@ -121,6 +121,14 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) error { ...@@ -121,6 +121,14 @@ func (c *TCPConn) SetKeepAlive(keepalive bool) error {
return setKeepAlive(c.fd, keepalive) 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 // SetNoDelay controls whether the operating system should delay
// packet transmission in hopes of sending fewer packets (Nagle's // packet transmission in hopes of sending fewer packets (Nagle's
// algorithm). The default is true (no delay), meaning that data is // algorithm). The default is true (no delay), meaning that data is
......
// 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))
}
// 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))
}
// 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))
}
// 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
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment