mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:24:01 +03:00
Merge pull request #142 from 9seconds/keep-alives
Set keep-alive for tcp connections
This commit is contained in:
@@ -3,10 +3,13 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/9seconds/mtg/config"
|
"github.com/9seconds/mtg/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const tcpKeepAlivePingPeriod = 2 * time.Second
|
||||||
|
|
||||||
func InitTCP(conn net.Conn) error {
|
func InitTCP(conn net.Conn) error {
|
||||||
tcpConn := conn.(*net.TCPConn)
|
tcpConn := conn.(*net.TCPConn)
|
||||||
|
|
||||||
@@ -22,5 +25,13 @@ func InitTCP(conn net.Conn) error {
|
|||||||
return fmt.Errorf("cannot set write buffer size: %w", err)
|
return fmt.Errorf("cannot set write buffer size: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := tcpConn.SetKeepAlive(true); err != nil {
|
||||||
|
return fmt.Errorf("cannot enable keep-alive: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tcpConn.SetKeepAlivePeriod(tcpKeepAlivePingPeriod); err != nil {
|
||||||
|
return fmt.Errorf("cannot set keep-alive period: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user