You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
680 B
Go
33 lines
680 B
Go
package graceful
|
|
|
|
import (
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
type keepAliveConn interface {
|
|
SetKeepAlive(bool) error
|
|
SetKeepAlivePeriod(d time.Duration) error
|
|
}
|
|
|
|
// keepAliveListener sets TCP keep-alive timeouts on accepted
|
|
// connections. It's used by ListenAndServe and ListenAndServeTLS so
|
|
// dead TCP connections (e.g. closing laptop mid-download) eventually
|
|
// go away.
|
|
type keepAliveListener struct {
|
|
net.Listener
|
|
keepAlivePeriod time.Duration
|
|
}
|
|
|
|
func (ln keepAliveListener) Accept() (net.Conn, error) {
|
|
c, err := ln.Listener.Accept()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
kac := c.(keepAliveConn)
|
|
kac.SetKeepAlive(true)
|
|
kac.SetKeepAlivePeriod(ln.keepAlivePeriod)
|
|
return c, nil
|
|
}
|