From cf259c3f22b8024440e8252ff9fa9a438d3f3679 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 13 Mar 2015 09:57:28 -0700 Subject: [PATCH] command/sever: copy the TCP keep alive listener --- command/server/listener_tcp.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/command/server/listener_tcp.go b/command/server/listener_tcp.go index 9f0325064f..151069b7c6 100644 --- a/command/server/listener_tcp.go +++ b/command/server/listener_tcp.go @@ -3,6 +3,7 @@ package server import ( "fmt" "net" + "time" ) func tcpListenerFactory(config map[string]string) (net.Listener, error) { @@ -16,5 +17,26 @@ func tcpListenerFactory(config map[string]string) (net.Listener, error) { return nil, err } + ln = tcpKeepAliveListener{ln.(*net.TCPListener)} return listenerWrapTLS(ln, config) } + +// 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. +// +// This is copied directly from the Go source code. +type tcpKeepAliveListener struct { + *net.TCPListener +} + +func (ln tcpKeepAliveListener) Accept() (c net.Conn, err error) { + tc, err := ln.AcceptTCP() + if err != nil { + return + } + tc.SetKeepAlive(true) + tc.SetKeepAlivePeriod(3 * time.Minute) + return tc, nil +}