github.com/searKing/golang/go@v1.2.117/net/tcp/listen.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package tcp 6 7 import ( 8 "net" 9 "time" 10 ) 11 12 // tcpKeepAliveListener sets TCP keep-alive timeouts on accepted 13 // connections. It's used by ListenAndServe and ListenAndServeTLS so 14 // dead TCP connections (e.g. closing laptop mid-download) eventually 15 // go away. 16 type tcpKeepAliveListener struct { 17 *net.TCPListener 18 } 19 20 func (ln tcpKeepAliveListener) Accept() (net.Conn, error) { 21 tc, err := ln.AcceptTCP() 22 if err != nil { 23 return nil, err 24 } 25 tc.SetKeepAlive(true) 26 tc.SetKeepAlivePeriod(3 * time.Minute) 27 return tc, nil 28 }