Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tcpkeepalive_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tcpkeepalive

import (
"os"
"runtime"
"syscall"
"time"
)

const sysTCP_KEEPINTVL = 0x101

func setIdle(fd uintptr, d time.Duration) error {
// not possible with darwin
return nil
}

func setCount(fd uintptr, n int) error {
// not possible with darwin
Copy link
Member

@sgotti sgotti Oct 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the source code https://github.com/apple/darwin-xnu/blob/0a798f6738bc1db01281fc08ae024145e84df927/bsd/netinet/tcp.h looks like also count is implemented on darwin (not sure from which version)

In this repo that uses the hackish way of calling setsockopt they are using it:
https://github.com/felixge/tcpkeepalive/blob/master/keepalive_darwin.go

return nil
}

func setInterval(fd uintptr, d time.Duration) error {
// # from https://golang.org/src/net/tcpsockopt_darwin.go
d += (time.Second - time.Nanosecond)
secs := int(d.Seconds())
switch err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err {
case nil, syscall.ENOPROTOOPT: // OS X 10.7 and earlier don't support this option
default:
return os.NewSyscallError("setsockopt", err)
}
err := syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs)
Copy link
Member

@sgotti sgotti Oct 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the man https://github.com/apple/darwin-xnu/blob/5394bb038891708cd4ba748da79b90a33b19f82e/bsd/man/man4/tcp.4#L172 TCP_KEEPALIVE should set the idle and not the interval so it doesn't belong here but should be put in the setIdle function.

runtime.KeepAlive(fd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't needed since the rawConn interface alreafy guarantees that the file descriptor will remain valid https://golang.org/pkg/syscall/#RawConn

return os.NewSyscallError("setsockopt", err)
}