Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Extract keepAliveListener
Browse files Browse the repository at this point in the history
  • Loading branch information
djui committed Jul 8, 2016
1 parent 3e03335 commit ee93aaa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
26 changes: 0 additions & 26 deletions graceful.go
Original file line number Diff line number Diff line change
Expand Up @@ -437,29 +437,3 @@ func (srv *Server) shutdown(shutdown chan chan struct{}, kill chan struct{}) {
}
srv.chanLock.Unlock()
}

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
}
32 changes: 32 additions & 0 deletions keepalive_listener.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit ee93aaa

Please sign in to comment.