|
|
@@ -3,10 +3,13 @@ package utils
|
|
3
|
3
|
import (
|
|
4
|
4
|
"fmt"
|
|
5
|
5
|
"net"
|
|
|
6
|
+ "time"
|
|
6
|
7
|
|
|
7
|
8
|
"github.com/9seconds/mtg/config"
|
|
8
|
9
|
)
|
|
9
|
10
|
|
|
|
11
|
+const tcpKeepAlivePingPeriod = 2 * time.Second
|
|
|
12
|
+
|
|
10
|
13
|
func InitTCP(conn net.Conn) error {
|
|
11
|
14
|
tcpConn := conn.(*net.TCPConn)
|
|
12
|
15
|
|
|
|
@@ -22,5 +25,13 @@ func InitTCP(conn net.Conn) error {
|
|
22
|
25
|
return fmt.Errorf("cannot set write buffer size: %w", err)
|
|
23
|
26
|
}
|
|
24
|
27
|
|
|
|
28
|
+ if err := tcpConn.SetKeepAlive(true); err != nil {
|
|
|
29
|
+ return fmt.Errorf("cannot enable keep-alive: %w", err)
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ if err := tcpConn.SetKeepAlivePeriod(tcpKeepAlivePingPeriod); err != nil {
|
|
|
33
|
+ return fmt.Errorf("cannot set keep-alive period: %w", err)
|
|
|
34
|
+ }
|
|
|
35
|
+
|
|
25
|
36
|
return nil
|
|
26
|
37
|
}
|