amuz.es/src/infra/goutils@v0.1.3/http/tcp_listener.go (about)

     1  package http
     2  
     3  import (
     4  	"time"
     5  	"net"
     6  )
     7  
     8  func NewListener(addr *net.TCPAddr) (net.Listener, error) {
     9  	listener, err := net.ListenTCP("tcp", addr)
    10  	if err != nil {
    11  		return nil, err
    12  	}
    13  
    14  	return &keepAliveListener{Listener: listener}, nil
    15  
    16  }
    17  
    18  type keepAliveListener struct {
    19  	// the number of open connections
    20  	net.Listener
    21  }
    22  
    23  func (ln *keepAliveListener) Accept() (c net.Conn, err error) {
    24  	c, err = ln.Listener.Accept()
    25  	if err != nil {
    26  		return
    27  	}
    28  	if tc, ok := c.(*net.TCPConn); ok {
    29  		tc.SetKeepAlive(true)
    30  		tc.SetKeepAlivePeriod(3 * time.Minute)
    31  	}
    32  	return
    33  }