syncthing/cmd/strelaysrv/status.go

112 lines
2.9 KiB
Go
Raw Normal View History

2016-06-02 05:16:02 -07:00
// Copyright (C) 2015 Audrius Butkevicius and Contributors.
2015-08-20 03:59:44 -07:00
package main
import (
"encoding/json"
"log"
"net/http"
"runtime"
"sync/atomic"
2015-08-20 05:02:52 -07:00
"time"
2015-08-20 03:59:44 -07:00
)
2015-08-20 05:02:52 -07:00
var rc *rateCalculator
2015-08-20 03:59:44 -07:00
func statusService(addr string) {
2015-08-20 05:02:52 -07:00
rc = newRateCalculator(360, 10*time.Second, &bytesProxied)
2015-08-20 03:59:44 -07:00
http.HandleFunc("/status", getStatus)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatal(err)
}
}
func getStatus(w http.ResponseWriter, r *http.Request) {
2015-10-22 13:44:29 -07:00
w.Header().Set("Access-Control-Allow-Origin", "*")
2015-08-20 03:59:44 -07:00
status := make(map[string]interface{})
sessionMut.Lock()
// This can potentially be double the number of pending sessions, as each session has two keys, one for each side.
2015-10-10 03:43:07 -07:00
status["startTime"] = rc.startTime
2015-10-22 13:40:36 -07:00
status["uptimeSeconds"] = time.Since(rc.startTime) / time.Second
status["numPendingSessionKeys"] = len(pendingSessions)
status["numActiveSessions"] = len(activeSessions)
2015-08-20 03:59:44 -07:00
sessionMut.Unlock()
2015-08-20 05:02:52 -07:00
status["numConnections"] = atomic.LoadInt64(&numConnections)
2015-08-20 03:59:44 -07:00
status["numProxies"] = atomic.LoadInt64(&numProxies)
status["bytesProxied"] = atomic.LoadInt64(&bytesProxied)
status["goVersion"] = runtime.Version()
status["goOS"] = runtime.GOOS
2015-11-21 11:58:52 -07:00
status["goArch"] = runtime.GOARCH
2015-08-20 03:59:44 -07:00
status["goMaxProcs"] = runtime.GOMAXPROCS(-1)
2016-02-10 14:24:57 -07:00
status["goNumRoutine"] = runtime.NumGoroutine()
2015-08-20 05:02:52 -07:00
status["kbps10s1m5m15m30m60m"] = []int64{
rc.rate(10/10) * 8 / 1000,
rc.rate(60/10) * 8 / 1000,
rc.rate(5*60/10) * 8 / 1000,
rc.rate(15*60/10) * 8 / 1000,
rc.rate(30*60/10) * 8 / 1000,
rc.rate(60*60/10) * 8 / 1000,
}
2015-10-10 03:49:34 -07:00
status["options"] = map[string]interface{}{
2015-10-22 13:40:36 -07:00
"network-timeout": networkTimeout / time.Second,
"ping-interval": pingInterval / time.Second,
"message-timeout": messageTimeout / time.Second,
2015-10-10 03:49:34 -07:00
"per-session-rate": sessionLimitBps,
"global-rate": globalLimitBps,
2015-10-22 13:40:36 -07:00
"pools": pools,
2015-10-18 15:15:01 -07:00
"provided-by": providedBy,
2015-10-10 03:49:34 -07:00
}
2015-08-20 03:59:44 -07:00
bs, err := json.MarshalIndent(status, "", " ")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(bs)
}
2015-08-20 05:02:52 -07:00
type rateCalculator struct {
2015-10-10 03:43:07 -07:00
rates []int64
prev int64
counter *int64
startTime time.Time
2015-08-20 05:02:52 -07:00
}
func newRateCalculator(keepIntervals int, interval time.Duration, counter *int64) *rateCalculator {
r := &rateCalculator{
2015-10-10 03:43:07 -07:00
rates: make([]int64, keepIntervals),
counter: counter,
startTime: time.Now(),
2015-08-20 05:02:52 -07:00
}
go r.updateRates(interval)
return r
}
func (r *rateCalculator) updateRates(interval time.Duration) {
for {
now := time.Now()
next := now.Truncate(interval).Add(interval)
time.Sleep(next.Sub(now))
cur := atomic.LoadInt64(r.counter)
rate := int64(float64(cur-r.prev) / interval.Seconds())
copy(r.rates[1:], r.rates)
r.rates[0] = rate
r.prev = cur
}
}
func (r *rateCalculator) rate(periods int) int64 {
var tot int64
for i := 0; i < periods; i++ {
tot += r.rates[i]
}
return tot / int64(periods)
}