-
Notifications
You must be signed in to change notification settings - Fork 4
/
tcpkeepalive.go
46 lines (40 loc) · 1021 Bytes
/
tcpkeepalive.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// +build go1.9
package tcpkeepalive
import (
"net"
"time"
)
// SetKeepAliveIdle sets the time the connection must be idle before keepalive
// probes are sent.
func SetKeepAliveIdle(c *net.TCPConn, d time.Duration) error {
return control(c, func(fd uintptr) error {
return setIdle(fd, d)
})
}
// SetKeepAliveCount sets the number of keepalive probes without an acknowledge
// TCP should send before dropping the connection.
func SetKeepAliveCount(c *net.TCPConn, n int) error {
return control(c, func(fd uintptr) error {
return setCount(fd, n)
})
}
// SetKeepAliveInterval sets the time between keepalive probes.
func SetKeepAliveInterval(c *net.TCPConn, d time.Duration) error {
return control(c, func(fd uintptr) error {
return setInterval(fd, d)
})
}
func control(c *net.TCPConn, f func(fd uintptr) error) error {
rc, err := c.SyscallConn()
if err != nil {
return err
}
var operr error
err = rc.Control(func(fd uintptr) {
operr = f(fd)
})
if err != nil {
return err
}
return operr
}