stats.go 872 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package main
  2. // This code handles periodic statistics logging.
  3. //
  4. // The only thing it keeps track of is how many connections had the client_ip
  5. // parameter. Write true to statsChannel to record a connection with client_ip;
  6. // write false for without.
  7. import (
  8. "log"
  9. "time"
  10. )
  11. const (
  12. statsInterval = 24 * time.Hour
  13. )
  14. var (
  15. statsChannel = make(chan bool)
  16. )
  17. func statsThread() {
  18. var numClientIP, numConnections uint64
  19. prevTime := time.Now()
  20. deadline := time.After(statsInterval)
  21. for {
  22. select {
  23. case v := <-statsChannel:
  24. if v {
  25. numClientIP++
  26. }
  27. numConnections++
  28. case <-deadline:
  29. now := time.Now()
  30. log.Printf("in the past %.f s, %d/%d connections had client_ip",
  31. (now.Sub(prevTime)).Seconds(),
  32. numClientIP, numConnections)
  33. numClientIP = 0
  34. numConnections = 0
  35. prevTime = now
  36. deadline = time.After(statsInterval)
  37. }
  38. }
  39. }