Refactor(proxy): replace with safeConnClose

This commit is contained in:
xjasonlyu
2021-07-16 17:51:58 +08:00
parent c2ad0930b0
commit 33e2aed86c
3 changed files with 9 additions and 10 deletions

25
proxy/util.go Executable file
View File

@@ -0,0 +1,25 @@
package proxy
import (
"net"
"time"
)
const (
tcpKeepAlivePeriod = 30 * time.Second
)
// setKeepAlive sets tcp keepalive option for tcp connection.
func setKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
tcp.SetKeepAlivePeriod(tcpKeepAlivePeriod)
}
}
// safeConnClose closes tcp connection safely.
func safeConnClose(c net.Conn, err error) {
if c != nil && err != nil {
c.Close()
}
}