code refactor for common package

This commit is contained in:
ginuerzh
2021-11-15 13:09:45 +08:00
parent ce3d62759a
commit 2c0ce35b0b
40 changed files with 61 additions and 61 deletions

32
pkg/common/util/tcp.go Normal file
View File

@ -0,0 +1,32 @@
package util
import (
"net"
"time"
)
const (
defaultKeepAlivePeriod = 180 * time.Second
)
// TCPKeepAliveListener is a TCP listener with keep alive enabled.
type TCPKeepAliveListener struct {
KeepAlivePeriod time.Duration
*net.TCPListener
}
func (l *TCPKeepAliveListener) Accept() (c net.Conn, err error) {
tc, err := l.AcceptTCP()
if err != nil {
return
}
tc.SetKeepAlive(true)
period := l.KeepAlivePeriod
if period <= 0 {
period = defaultKeepAlivePeriod
}
tc.SetKeepAlivePeriod(period)
return tc, nil
}