tracker/server/stats.go

40 lines
877 B
Go
Raw Normal View History

2013-06-23 09:56:28 +02:00
// Copyright 2013 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package server
import (
2013-11-24 06:49:20 +01:00
"encoding/json"
"net/http"
"sync/atomic"
"time"
2013-06-23 09:56:28 +02:00
2013-11-24 06:49:20 +01:00
"github.com/pushrax/chihaya/config"
2013-06-23 09:56:28 +02:00
)
type stats struct {
2013-11-24 06:49:20 +01:00
Uptime config.Duration `json:"uptime"`
RPM int64 `json:"req_per_min"`
2013-06-23 09:56:28 +02:00
}
func (s *Server) serveStats(w http.ResponseWriter, r *http.Request) {
2013-11-24 06:49:20 +01:00
w.Header().Set("Content-Type", "application/json")
2013-11-24 06:49:20 +01:00
stats, _ := json.Marshal(&stats{
config.Duration{time.Now().Sub(s.startTime)},
s.rpm,
})
2013-11-24 06:49:20 +01:00
length, _ := w.Write(stats)
w.Header().Set("Content-Length", string(length))
w.(http.Flusher).Flush()
2013-06-23 09:56:28 +02:00
}
2013-08-13 08:38:00 +02:00
func (s *Server) updateStats() {
2013-11-24 06:49:20 +01:00
for _ = range time.NewTicker(time.Minute).C {
s.rpm = s.deltaRequests
atomic.StoreInt64(&s.deltaRequests, 0)
}
2013-06-23 09:56:28 +02:00
}