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.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Activates a registered client, e.g., for the initial attach to a persistent client
  24. func (s *Stats) AddRegistered(invisible, operator bool) {
  25. s.mutex.Lock()
  26. if invisible {
  27. s.Invisible += 1
  28. }
  29. if operator {
  30. s.Operators += 1
  31. }
  32. s.Total += 1
  33. s.setMax()
  34. s.mutex.Unlock()
  35. }
  36. // Transition a client from unregistered to registered
  37. func (s *Stats) Register(invisible bool) {
  38. s.mutex.Lock()
  39. s.Unknown -= 1
  40. if invisible {
  41. s.Invisible += 1
  42. }
  43. s.Total += 1
  44. s.setMax()
  45. s.mutex.Unlock()
  46. }
  47. func (s *Stats) setMax() {
  48. if s.Max < s.Total {
  49. s.Max = s.Total
  50. }
  51. }
  52. // Modify the Invisible count
  53. func (s *Stats) ChangeInvisible(increment int) {
  54. s.mutex.Lock()
  55. s.Invisible += increment
  56. s.mutex.Unlock()
  57. }
  58. // Modify the Operator count
  59. func (s *Stats) ChangeOperators(increment int) {
  60. s.mutex.Lock()
  61. s.Operators += increment
  62. s.mutex.Unlock()
  63. }
  64. // Remove a user from the server
  65. func (s *Stats) Remove(registered, invisible, operator bool) {
  66. s.mutex.Lock()
  67. if registered {
  68. s.Total -= 1
  69. } else {
  70. s.Unknown -= 1
  71. }
  72. if invisible {
  73. s.Invisible -= 1
  74. }
  75. if operator {
  76. s.Operators -= 1
  77. }
  78. s.mutex.Unlock()
  79. }
  80. // GetStats retrives total, invisible and oper count
  81. func (s *Stats) GetValues() (result StatsValues) {
  82. s.mutex.Lock()
  83. result = s.StatsValues
  84. s.mutex.Unlock()
  85. return
  86. }