blob: 7cc716bad1042fdc17805cb95f4d245cdf266c37 [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2009 The Go Authors. All rights reserved.
Mikio Hara6a6224c2014-03-13 14:45:50 +09002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package net
6
7import (
8 "os"
9 "syscall"
10 "time"
11)
12
Mikio Hara6a6224c2014-03-13 14:45:50 +090013func setKeepAlivePeriod(fd *netFD, d time.Duration) error {
14 if err := fd.incref(); err != nil {
15 return err
16 }
17 defer fd.decref()
Mikio Haraf9567402014-09-11 17:56:58 +090018 // The kernel expects milliseconds so round to next highest
19 // millisecond.
Mikio Hara6a6224c2014-03-13 14:45:50 +090020 d += (time.Millisecond - time.Nanosecond)
Mikio Haraf9567402014-09-11 17:56:58 +090021 msecs := int(d / time.Millisecond)
22 if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPINTVL, msecs); err != nil {
23 return os.NewSyscallError("setsockopt", err)
Mikio Hara6a6224c2014-03-13 14:45:50 +090024 }
25 return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.TCP_KEEPIDLE, msecs))
26}