diff --git a/http/http.go b/http/http.go index aea253e..8759306 100644 --- a/http/http.go +++ b/http/http.go @@ -6,6 +6,7 @@ package http import ( + "net" "net/http" "time" @@ -14,6 +15,7 @@ import ( "github.com/stretchr/graceful" "github.com/chihaya/chihaya/config" + "github.com/chihaya/chihaya/stats" "github.com/chihaya/chihaya/tracker" ) @@ -73,6 +75,25 @@ func newRouter(s *Server) *httprouter.Router { return r } +func (s *Server) connState(conn net.Conn, state http.ConnState) { + switch state { + case http.StateNew: + stats.RecordEvent(stats.AcceptedConnection) + + case http.StateClosed: + stats.RecordEvent(stats.ClosedConnection) + + case http.StateHijacked: + panic("connection impossibly hijacked") + + case http.StateActive: // Ignore. + case http.StateIdle: // Ignore. + + default: + glog.Errorf("Connection transitioned to unknown state %s (%d)", state, state) + } +} + func Serve(cfg *config.Config, tkr *tracker.Tracker) { srv := &Server{ config: cfg, @@ -80,7 +101,18 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) { } glog.V(0).Info("Starting on ", cfg.Addr) - graceful.Run(cfg.Addr, cfg.RequestTimeout.Duration, newRouter(srv)) + + grace := graceful.Server{ + Timeout: cfg.RequestTimeout.Duration, + ConnState: srv.connState, + Server: &http.Server{ + Addr: cfg.Addr, + Handler: newRouter(srv), + }, + } + + grace.ListenAndServe() + err := srv.tracker.Close() if err != nil { glog.Errorf("Failed to shutdown tracker cleanly: %s", err.Error()) diff --git a/stats/stats.go b/stats/stats.go index c301656..99d460a 100644 --- a/stats/stats.go +++ b/stats/stats.go @@ -73,7 +73,7 @@ type Stats struct { TorrentsRemoved uint64 `json:"torrents_removed"` TorrentsReaped uint64 `json:"torrents_reaped"` - ActiveConnections uint64 `json:"active_connections"` + OpenConnections uint64 `json:"open_connections"` ConnectionsAccepted uint64 `json:"connections_accepted"` BytesTransmitted uint64 `json:"bytes_transmitted"` @@ -161,10 +161,10 @@ func (s *Stats) handleEvents() { case AcceptedConnection: s.ConnectionsAccepted++ - s.ActiveConnections++ + s.OpenConnections++ case ClosedConnection: - s.ActiveConnections-- + s.OpenConnections-- case HandledRequest: s.RequestsHandled++