LEFT | RIGHT |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package net | 5 package net |
6 | 6 |
7 import ( | 7 import ( |
8 "os" | 8 "os" |
9 "syscall" | 9 "syscall" |
10 "time" | 10 "time" |
11 ) | 11 ) |
12 | 12 |
13 const ( | 13 const sysTCP_KEEPINTVL = 0x101 |
14 » sysTCP_KEEPINTVL = 0x101 | |
15 » sysTCP_KEEPCNT = 0x102 | |
16 ) | |
17 | 14 |
18 func setKeepAlivePeriod(fd *netFD, d time.Duration) error { | 15 func setKeepAlivePeriod(fd *netFD, d time.Duration) error { |
19 if err := fd.incref(); err != nil { | 16 if err := fd.incref(); err != nil { |
20 return err | 17 return err |
21 } | 18 } |
22 defer fd.decref() | 19 defer fd.decref() |
23 » idleInterval := keepAliveInterval{ // [1 second, MaxUint32 milliseconds) | 20 » // The kernel expects seconds so round to next highest second. |
24 » » time.Second, | 21 » d += (time.Second - time.Nanosecond) |
25 » » (1<<32 - 2) * time.Millisecond, | 22 » secs := int(d.Seconds()) |
26 » } | 23 » switch err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTC
P_KEEPINTVL, secs); err { |
27 » probeInterval := keepAliveInterval{ // [1 second, MaxUint32 milliseconds
) | 24 » case nil, syscall.ENOPROTOOPT: // OS X 10.7 and earlier don't support th
is option |
28 » » time.Second, | 25 » default: |
29 » » (1<<32 - 2) * time.Millisecond, | |
30 » } | |
31 » idle, probe := keepAliveSubinterval(d, &idleInterval, &probeInterval) | |
32 » if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, syscall.T
CP_KEEPALIVE, int(idle.Seconds())); err != nil { | |
33 return os.NewSyscallError("setsockopt", err) | 26 return os.NewSyscallError("setsockopt", err) |
34 } | 27 } |
35 » if err := syscall.SetsockoptInt(fd.sysfd, syscall.IPPROTO_TCP, sysTCP_KE
EPCNT, int(probe/time.Second)); err != nil { | 28 » return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd,
syscall.IPPROTO_TCP, syscall.TCP_KEEPALIVE, secs)) |
36 » » return os.NewSyscallError("setsockopt", err) | |
37 » } | |
38 » return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd.sysfd,
syscall.IPPROTO_TCP, sysTCP_KEEPINTVL, 1)) | |
39 } | 29 } |
LEFT | RIGHT |