2021-05-03 21:04:48 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
2021-12-22 09:33:21 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-05-03 21:04:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// tcpKeepAliveListener 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.
|
2021-12-22 09:33:21 +00:00
|
|
|
type TCPKeepAliveListener struct {
|
2021-05-03 21:04:48 +00:00
|
|
|
*net.TCPListener
|
|
|
|
}
|
|
|
|
|
2021-12-22 09:33:21 +00:00
|
|
|
func (ln TCPKeepAliveListener) Accept() (net.Conn, error) {
|
2021-05-03 21:04:48 +00:00
|
|
|
tc, err := ln.AcceptTCP()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = tc.SetKeepAlive(true)
|
|
|
|
if err != nil {
|
2021-12-22 09:33:21 +00:00
|
|
|
log.WithError(err).Warning("Error setting Keep-Alive")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
|
|
|
err = tc.SetKeepAlivePeriod(3 * time.Minute)
|
|
|
|
if err != nil {
|
2021-12-22 09:33:21 +00:00
|
|
|
log.WithError(err).Warning("Error setting Keep-Alive period")
|
2021-05-03 21:04:48 +00:00
|
|
|
}
|
|
|
|
return tc, nil
|
|
|
|
}
|