David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 5 | package net |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | ) |
| 12 | |
Mikio Hara | f956740 | 2014-09-11 17:56:58 +0900 | [diff] [blame] | 13 | const sysTCP_KEEPINTVL = 0x101 |
| 14 | |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 15 | func setKeepAlivePeriod(fd *netFD, d time.Duration) error { |
Dmitriy Vyukov | 23e15f7 | 2013-08-09 21:43:00 +0400 | [diff] [blame] | 16 | if err := fd.incref(); err != nil { |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 17 | return err |
| 18 | } |
| 19 | defer fd.decref() |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 20 | // The kernel expects seconds so round to next highest second. |
| 21 | d += (time.Second - time.Nanosecond) |
| 22 | secs := int(d.Seconds()) |
Mikio Hara | f956740 | 2014-09-11 17:56:58 +0900 | [diff] [blame] | 23 | switch err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, secs); err { |
| 24 | case nil, syscall.ENOPROTOOPT: // OS X 10.7 and earlier don't support this option |
| 25 | default: |
| 26 | return os.NewSyscallError("setsockopt", err) |
| 27 | } |
David Presotto | 918922c | 2013-07-15 18:40:55 -0400 | [diff] [blame] | 28 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs)) |
| 29 | } |