You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stats.go 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package irc
  2. import (
  3. "sync"
  4. )
  5. type StatsValues struct {
  6. Unknown int // unregistered clients
  7. Total int // registered clients, including invisible
  8. Max int // high-water mark of registered clients
  9. Invisible int
  10. Operators int
  11. }
  12. // Stats tracks statistics for a running server
  13. type Stats struct {
  14. StatsValues
  15. mutex sync.Mutex
  16. }
  17. // Adds an unregistered client
  18. func (s *Stats) Add() {
  19. s.mutex.Lock()
  20. s.Unknown += 1
  21. s.mutex.Unlock()
  22. }
  23. // Transition a client from unregistered to registered
  24. func (s *Stats) Register() {
  25. s.mutex.Lock()
  26. s.Unknown -= 1
  27. s.Total += 1
  28. if s.Max < s.Total {
  29. s.Max = s.Total
  30. }
  31. s.mutex.Unlock()
  32. }
  33. // Modify the Invisible count
  34. func (s *Stats) ChangeInvisible(increment int) {
  35. s.mutex.Lock()
  36. s.Invisible += increment
  37. s.mutex.Unlock()
  38. }
  39. // Modify the Operator count
  40. func (s *Stats) ChangeOperators(increment int) {
  41. s.mutex.Lock()
  42. s.Operators += increment
  43. s.mutex.Unlock()
  44. }
  45. // Remove a user from the server
  46. func (s *Stats) Remove(registered, invisible, operator bool) {
  47. s.mutex.Lock()
  48. if registered {
  49. s.Total -= 1
  50. } else {
  51. s.Unknown -= 1
  52. }
  53. if invisible {
  54. s.Invisible -= 1
  55. }
  56. if operator {
  57. s.Operators -= 1
  58. }
  59. s.mutex.Unlock()
  60. }
  61. // GetStats retrives total, invisible and oper count
  62. func (s *Stats) GetValues() (result StatsValues) {
  63. s.mutex.Lock()
  64. result = s.StatsValues
  65. s.mutex.Unlock()
  66. return
  67. }