Skip to content

Commit 76e2ce0

Browse files
committed
Add control.SetKeepAlivePeriod
1 parent 0ea2687 commit 76e2ce0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package control
2+
3+
import (
4+
"syscall"
5+
"time"
6+
_ "unsafe"
7+
8+
E "github.com/sagernet/sing/common/exceptions"
9+
N "github.com/sagernet/sing/common/network"
10+
11+
"golang.org/x/sys/unix"
12+
)
13+
14+
func SetKeepAlivePeriod(idle time.Duration, interval time.Duration) Func {
15+
return func(network, address string, conn syscall.RawConn) error {
16+
if N.NetworkName(network) != N.NetworkTCP {
17+
return nil
18+
}
19+
return Raw(conn, func(fd uintptr) error {
20+
return E.Errors(
21+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPIDLE, int(roundDurationUp(idle, time.Second))),
22+
unix.SetsockoptInt(int(fd), unix.IPPROTO_TCP, unix.TCP_KEEPINTVL, int(roundDurationUp(interval, time.Second))),
23+
)
24+
})
25+
}
26+
}
27+
28+
//go:linkname roundDurationUp net.roundDurationUp
29+
func roundDurationUp(d time.Duration, to time.Duration) time.Duration
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !linux
2+
3+
package control
4+
5+
import (
6+
"time"
7+
)
8+
9+
func SetKeepAlivePeriod(idle time.Duration, interval time.Duration) Func {
10+
return nil
11+
}

0 commit comments

Comments
 (0)