Implement OpenConnections and ConnectionsAccepted stats

This commit is contained in:
Justin Li 2014-07-22 01:38:07 -04:00
parent 4514705363
commit 6fdb68d9b2
2 changed files with 36 additions and 4 deletions

View file

@ -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())

View file

@ -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++