選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

stats.go 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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() {
  38. s.mutex.Lock()
  39. s.Unknown -= 1
  40. s.Total += 1
  41. s.setMax()
  42. s.mutex.Unlock()
  43. }
  44. func (s *Stats) setMax() {
  45. if s.Max < s.Total {
  46. s.Max = s.Total
  47. }
  48. }
  49. // Modify the Invisible count
  50. func (s *Stats) ChangeInvisible(increment int) {
  51. s.mutex.Lock()
  52. s.Invisible += increment
  53. s.mutex.Unlock()
  54. }
  55. // Modify the Operator count
  56. func (s *Stats) ChangeOperators(increment int) {
  57. s.mutex.Lock()
  58. s.Operators += increment
  59. s.mutex.Unlock()
  60. }
  61. // Remove a user from the server
  62. func (s *Stats) Remove(registered, invisible, operator bool) {
  63. s.mutex.Lock()
  64. if registered {
  65. s.Total -= 1
  66. } else {
  67. s.Unknown -= 1
  68. }
  69. if invisible {
  70. s.Invisible -= 1
  71. }
  72. if operator {
  73. s.Operators -= 1
  74. }
  75. s.mutex.Unlock()
  76. }
  77. // GetStats retrives total, invisible and oper count
  78. func (s *Stats) GetValues() (result StatsValues) {
  79. s.mutex.Lock()
  80. result = s.StatsValues
  81. s.mutex.Unlock()
  82. return
  83. }