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 package http
import ( import (
"net"
"net/http" "net/http"
"time" "time"
@ -14,6 +15,7 @@ import (
"github.com/stretchr/graceful" "github.com/stretchr/graceful"
"github.com/chihaya/chihaya/config" "github.com/chihaya/chihaya/config"
"github.com/chihaya/chihaya/stats"
"github.com/chihaya/chihaya/tracker" "github.com/chihaya/chihaya/tracker"
) )
@ -73,6 +75,25 @@ func newRouter(s *Server) *httprouter.Router {
return r 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) { func Serve(cfg *config.Config, tkr *tracker.Tracker) {
srv := &Server{ srv := &Server{
config: cfg, config: cfg,
@ -80,7 +101,18 @@ func Serve(cfg *config.Config, tkr *tracker.Tracker) {
} }
glog.V(0).Info("Starting on ", cfg.Addr) 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() err := srv.tracker.Close()
if err != nil { if err != nil {
glog.Errorf("Failed to shutdown tracker cleanly: %s", err.Error()) glog.Errorf("Failed to shutdown tracker cleanly: %s", err.Error())

View file

@ -73,7 +73,7 @@ type Stats struct {
TorrentsRemoved uint64 `json:"torrents_removed"` TorrentsRemoved uint64 `json:"torrents_removed"`
TorrentsReaped uint64 `json:"torrents_reaped"` TorrentsReaped uint64 `json:"torrents_reaped"`
ActiveConnections uint64 `json:"active_connections"` OpenConnections uint64 `json:"open_connections"`
ConnectionsAccepted uint64 `json:"connections_accepted"` ConnectionsAccepted uint64 `json:"connections_accepted"`
BytesTransmitted uint64 `json:"bytes_transmitted"` BytesTransmitted uint64 `json:"bytes_transmitted"`
@ -161,10 +161,10 @@ func (s *Stats) handleEvents() {
case AcceptedConnection: case AcceptedConnection:
s.ConnectionsAccepted++ s.ConnectionsAccepted++
s.ActiveConnections++ s.OpenConnections++
case ClosedConnection: case ClosedConnection:
s.ActiveConnections-- s.OpenConnections--
case HandledRequest: case HandledRequest:
s.RequestsHandled++ s.RequestsHandled++