summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/tylerb/graceful/keepalive_listener.go
blob: 1484bc213aaa3e6e24805a6277e501c8f246222f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
}