throttle.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package ircclient
  2. import (
  3. "log"
  4. "time"
  5. )
  6. type FloodTrack struct {
  7. Pos int
  8. Size int
  9. Timeout int
  10. Track []time.Time
  11. }
  12. func (F *FloodTrack) Init(size int, timeout int) {
  13. F.Size = size
  14. F.Timeout = timeout
  15. F.Track = make([]time.Time, size)
  16. }
  17. func (F *FloodTrack) Full() bool {
  18. if F.Pos > 0 {
  19. F.Expire()
  20. }
  21. return F.Pos == F.Size
  22. }
  23. func (F *FloodTrack) Expire() {
  24. var idx int
  25. ReCheck:
  26. for idx = 0; idx < F.Pos; idx++ {
  27. // log.Println(idx, time.Since(F.Track[idx]).Seconds())
  28. if time.Since(F.Track[idx]).Seconds() > float64(F.Timeout) {
  29. // Remove this from the list
  30. F.Pos--
  31. for pos := idx; pos < F.Pos; pos++ {
  32. F.Track[pos] = F.Track[pos+1]
  33. }
  34. goto ReCheck
  35. }
  36. }
  37. }
  38. func (F *FloodTrack) Save() {
  39. F.Track[F.Pos] = time.Now()
  40. F.Pos++
  41. }
  42. type ThrottleBuffer struct {
  43. buffer map[string][]string
  44. targets []string
  45. last int
  46. Life_sucks bool
  47. }
  48. func (T *ThrottleBuffer) init() {
  49. T.buffer = make(map[string][]string, 0)
  50. T.targets = make([]string, 0)
  51. T.last = 0
  52. }
  53. /*
  54. Pop next available from buffer. If empty, remove from map and targets
  55. list. Adjust last if needed.
  56. */
  57. func (T *ThrottleBuffer) pop() string {
  58. // Get next target
  59. var t string = T.targets[T.last]
  60. T.last++
  61. if T.last == len(T.targets) {
  62. // We're past the end, start over at the beginning.
  63. T.last = 0
  64. }
  65. var msg string = T.buffer[t][0]
  66. if len(T.buffer[t]) == 1 {
  67. // This is the last entry for this target.
  68. delete(T.buffer, t)
  69. if len(T.targets) == 1 {
  70. // This is the last entry
  71. T.targets = make([]string, 0)
  72. T.Life_sucks = false
  73. log.Println("Flood control off.")
  74. } else {
  75. // Remove t from targets
  76. for x := 0; x < len(T.targets); x++ {
  77. if T.targets[x] == t {
  78. T.targets = append(T.targets[:x], T.targets[x+1:]...)
  79. if x <= T.last {
  80. T.last--
  81. }
  82. break
  83. }
  84. }
  85. }
  86. } else {
  87. // Delete the first entry for t
  88. T.buffer[t] = append(T.buffer[t][:0], T.buffer[t][1:]...)
  89. }
  90. return msg
  91. }
  92. func (T *ThrottleBuffer) push(To string, Output string) {
  93. if len(T.targets) == 0 {
  94. T.Life_sucks = true
  95. T.last = 0
  96. log.Println("Flood control enabled.")
  97. }
  98. _, has := T.buffer[To]
  99. if !has {
  100. T.buffer[To] = make([]string, 0)
  101. T.targets = append(T.targets, To)
  102. }
  103. T.buffer[To] = append(T.buffer[To], Output)
  104. }
  105. func (T *ThrottleBuffer) delete(To string) {
  106. _, has := T.buffer[To]
  107. if has {
  108. // Yes, the buffer has message(s) for To
  109. delete(T.buffer, To)
  110. if len(T.targets) == 1 {
  111. // Last entry
  112. T.targets = make([]string, 0)
  113. T.Life_sucks = false
  114. log.Println("Flood control off.")
  115. } else {
  116. // Remove To from targets
  117. for x := 0; x < len(T.targets); x++ {
  118. if T.targets[x] == To {
  119. T.targets = append(T.targets[:x], T.targets[x+1:]...)
  120. // Don't decrement if already at first item
  121. if (x <= T.last) && (T.last != 0) {
  122. T.last--
  123. }
  124. break
  125. }
  126. }
  127. }
  128. }
  129. }