date_counter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2017 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package metric
  15. import (
  16. "sync"
  17. "time"
  18. )
  19. type DateCounter interface {
  20. TodayCount() int64
  21. GetLastDaysCount(lastdays int64) []int64
  22. Inc(int64)
  23. Dec(int64)
  24. Snapshot() DateCounter
  25. Clear()
  26. Close()
  27. }
  28. func NewDateCounter(reserveDays int64) DateCounter {
  29. if reserveDays <= 0 {
  30. reserveDays = 1
  31. }
  32. return newStandardDateCounter(reserveDays)
  33. }
  34. type StandardDateCounter struct {
  35. reserveDays int64
  36. counts []int64
  37. closeCh chan struct{}
  38. closed bool
  39. mu sync.Mutex
  40. }
  41. func newStandardDateCounter(reserveDays int64) *StandardDateCounter {
  42. s := &StandardDateCounter{
  43. reserveDays: reserveDays,
  44. counts: make([]int64, reserveDays),
  45. closeCh: make(chan struct{}),
  46. }
  47. s.startRotateWorker()
  48. return s
  49. }
  50. func (c *StandardDateCounter) TodayCount() int64 {
  51. c.mu.Lock()
  52. defer c.mu.Unlock()
  53. return c.counts[0]
  54. }
  55. func (c *StandardDateCounter) GetLastDaysCount(lastdays int64) []int64 {
  56. if lastdays > c.reserveDays {
  57. lastdays = c.reserveDays
  58. }
  59. counts := make([]int64, lastdays)
  60. c.mu.Lock()
  61. defer c.mu.Unlock()
  62. for i := 0; i < int(lastdays); i++ {
  63. counts[i] = c.counts[i]
  64. }
  65. return counts
  66. }
  67. func (c *StandardDateCounter) Inc(count int64) {
  68. c.mu.Lock()
  69. defer c.mu.Unlock()
  70. c.counts[0] += count
  71. }
  72. func (c *StandardDateCounter) Dec(count int64) {
  73. c.mu.Lock()
  74. defer c.mu.Unlock()
  75. c.counts[0] -= count
  76. }
  77. func (c *StandardDateCounter) Snapshot() DateCounter {
  78. c.mu.Lock()
  79. defer c.mu.Unlock()
  80. tmp := &StandardDateCounter{
  81. reserveDays: c.reserveDays,
  82. counts: make([]int64, c.reserveDays),
  83. }
  84. for i := 0; i < int(c.reserveDays); i++ {
  85. tmp.counts[i] = c.counts[i]
  86. }
  87. return tmp
  88. }
  89. func (c *StandardDateCounter) Clear() {
  90. c.mu.Lock()
  91. defer c.mu.Unlock()
  92. for i := 0; i < int(c.reserveDays); i++ {
  93. c.counts[i] = 0
  94. }
  95. }
  96. func (c *StandardDateCounter) Close() {
  97. c.mu.Lock()
  98. defer c.mu.Unlock()
  99. if !c.closed {
  100. close(c.closeCh)
  101. c.closed = true
  102. }
  103. }
  104. func (c *StandardDateCounter) rotate() {
  105. c.mu.Lock()
  106. defer c.mu.Unlock()
  107. newCounts := make([]int64, c.reserveDays)
  108. for i := 1; i < int(c.reserveDays-1); i++ {
  109. newCounts[i] = c.counts[i+1]
  110. }
  111. c.counts = newCounts
  112. }
  113. func (c *StandardDateCounter) startRotateWorker() {
  114. now := time.Now()
  115. nextDayTimeStr := now.Add(24 * time.Hour).Format("20060102")
  116. nextDay, _ := time.Parse("20060102", nextDayTimeStr)
  117. d := nextDay.Sub(now)
  118. firstTimer := time.NewTimer(d)
  119. rotateTicker := time.NewTicker(24 * time.Hour)
  120. go func() {
  121. for {
  122. select {
  123. case <-firstTimer.C:
  124. firstTimer.Stop()
  125. rotateTicker.Stop()
  126. rotateTicker = time.NewTicker(24 * time.Hour)
  127. c.rotate()
  128. case <-rotateTicker.C:
  129. c.rotate()
  130. case <-c.closeCh:
  131. break
  132. }
  133. }
  134. firstTimer.Stop()
  135. rotateTicker.Stop()
  136. }()
  137. }