Set keepalive for TCP sockets

This commit is contained in:
Can ZHANG 2017-05-16 15:10:56 +08:00
parent a91eba7f97
commit 986e17c2b6
1 changed files with 6 additions and 0 deletions

View File

@ -162,6 +162,12 @@ func (u *Upgrader) Upgrade(w http.ResponseWriter, r *http.Request, responseHeade
}
var brw *bufio.ReadWriter
netConn, brw, err = h.Hijack()
tcpConn, ok := netConn.(*net.TCPConn)
if ok {
// Set TCP keepalive for connections
tcpConn.SetKeepAlive(true)
tcpConn.SetKeepAlivePeriod(10 * time.Second)
}
if err != nil {
return u.returnError(w, r, http.StatusInternalServerError, err.Error())
}