2017-10-17 21:45:36 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
// This code handled periodic statistics logging.
|
|
|
|
|
//
|
|
|
|
|
// The only thing it keeps track of is how many connections had the client_ip
|
|
|
|
|
// parameter. Write true to statsChannel to record a connection with client_ip;
|
|
|
|
|
// write false for without.
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"log"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
statsInterval = 24 * time.Hour
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
statsChannel = make(chan bool)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func statsThread() {
|
|
|
|
|
var numClientIP, numConnections uint64
|
|
|
|
|
prevTime := time.Now()
|
2017-10-19 00:00:26 -07:00
|
|
|
deadline := time.After(statsInterval)
|
2017-10-17 21:45:36 -07:00
|
|
|
for {
|
|
|
|
|
select {
|
|
|
|
|
case v := <-statsChannel:
|
|
|
|
|
if v {
|
2019-09-18 16:23:30 +10:00
|
|
|
numClientIP++
|
2017-10-17 21:45:36 -07:00
|
|
|
}
|
2019-09-18 16:23:30 +10:00
|
|
|
numConnections++
|
2017-10-19 00:00:26 -07:00
|
|
|
case <-deadline:
|
2017-10-17 21:45:36 -07:00
|
|
|
now := time.Now()
|
2017-10-20 12:25:19 -07:00
|
|
|
log.Printf("in the past %.f s, %d/%d connections had client_ip",
|
2017-10-17 21:45:36 -07:00
|
|
|
(now.Sub(prevTime)).Seconds(),
|
|
|
|
|
numClientIP, numConnections)
|
|
|
|
|
numClientIP = 0
|
|
|
|
|
numConnections = 0
|
|
|
|
|
prevTime = now
|
2017-10-19 00:00:26 -07:00
|
|
|
deadline = time.After(statsInterval)
|
2017-10-17 21:45:36 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|