Colin Cross | 7bb052a | 2015-02-03 12:59:37 -0800 | [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 | |
| 5 | package net |
| 6 | |
| 7 | import ( |
| 8 | "os" |
| 9 | "syscall" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | const sysTCP_KEEPINTVL = 0x101 |
| 14 | |
| 15 | func setKeepAlivePeriod(fd *netFD, d time.Duration) error { |
| 16 | if err := fd.incref(); err != nil { |
| 17 | return err |
| 18 | } |
| 19 | defer fd.decref() |
| 20 | // The kernel expects seconds so round to next highest second. |
| 21 | d += (time.Second - time.Nanosecond) |
| 22 | secs := int(d.Seconds()) |
| 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 | } |
| 28 | return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs)) |
| 29 | } |