Przeglądaj źródła

Set keep-alive for tcp connections

tags/v1.0.4^2
9seconds 6 lat temu
rodzic
commit
850bed0dfc
1 zmienionych plików z 11 dodań i 0 usunięć
  1. 11
    0
      utils/init_tcp.go

+ 11
- 0
utils/init_tcp.go Wyświetl plik

3
 import (
3
 import (
4
 	"fmt"
4
 	"fmt"
5
 	"net"
5
 	"net"
6
+	"time"
6
 
7
 
7
 	"github.com/9seconds/mtg/config"
8
 	"github.com/9seconds/mtg/config"
8
 )
9
 )
9
 
10
 
11
+const tcpKeepAlivePingPeriod = 2 * time.Second
12
+
10
 func InitTCP(conn net.Conn) error {
13
 func InitTCP(conn net.Conn) error {
11
 	tcpConn := conn.(*net.TCPConn)
14
 	tcpConn := conn.(*net.TCPConn)
12
 
15
 
22
 		return fmt.Errorf("cannot set write buffer size: %w", err)
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
 	return nil
36
 	return nil
26
 }
37
 }

Ładowanie…
Anuluj
Zapisz