Skip to content

Commit

Permalink
Close idle connections after graceful shutdown timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tpng committed Jun 7, 2016
1 parent b23eec4 commit 37ae210
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions caddyhttp/httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func NewServer(addr string, group []*SiteConfig) (*Server, error) {
connTimeout: GracefulTimeout,
}
s.Server.Handler = s // this is weird, but whatever
s.Server.ConnState = func(c net.Conn, cs http.ConnState) {
if cs == http.StateIdle {
s.listenerMu.Lock()
// server stopped, close idle connection
if s.listener == nil {
c.Close()
}
s.listenerMu.Unlock()
}
}

// Disable HTTP/2 if desired
if !HTTP2 {
Expand Down Expand Up @@ -288,6 +298,7 @@ func (s *Server) Stop() (err error) {
s.listenerMu.Lock()
if s.listener != nil {
err = s.listener.Close()
s.listener = nil
}
s.listenerMu.Unlock()

Expand Down

0 comments on commit 37ae210

Please sign in to comment.