|
| 1 | +package timer |
| 2 | + |
| 3 | +import ( |
| 4 | + "container/heap" |
| 5 | + "context" |
| 6 | + "math" |
| 7 | + "sync" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +var _ Timer = (*minHeap)(nil) |
| 12 | + |
| 13 | +type minHeap struct { |
| 14 | + mu sync.Mutex |
| 15 | + minHeaps |
| 16 | + chAdd chan struct{} |
| 17 | + ctx context.Context |
| 18 | + cancel context.CancelFunc |
| 19 | + wait sync.WaitGroup |
| 20 | +} |
| 21 | + |
| 22 | +// 一次性定时器 |
| 23 | +func (m *minHeap) AfterFunc(expire time.Duration, callback func()) TimeNoder { |
| 24 | + return m.addCallback(expire, callback, false) |
| 25 | +} |
| 26 | + |
| 27 | +// 加任务 |
| 28 | +func (m *minHeap) addCallback(expire time.Duration, callback func(), isSchedule bool) TimeNoder { |
| 29 | + m.mu.Lock() |
| 30 | + defer m.mu.Unlock() |
| 31 | + |
| 32 | + node := minHeapNode{ |
| 33 | + callback: callback, |
| 34 | + userExpire: expire, |
| 35 | + absExpire: time.Now().Add(expire), |
| 36 | + isSchedule: isSchedule, |
| 37 | + root: m, |
| 38 | + } |
| 39 | + |
| 40 | + heap.Push(&m.minHeaps, node) |
| 41 | + select { |
| 42 | + case m.chAdd <- struct{}{}: |
| 43 | + default: |
| 44 | + } |
| 45 | + |
| 46 | + return &node |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +func (m *minHeap) removeTimeNode(node *minHeapNode) { |
| 51 | + m.mu.Lock() |
| 52 | + if node.index < 0 || node.index > len(m.minHeaps) || len(m.minHeaps) == 0 { |
| 53 | + m.mu.Unlock() |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + heap.Remove(&m.minHeaps, node.index) |
| 58 | + m.mu.Unlock() |
| 59 | +} |
| 60 | + |
| 61 | +// 周期性定时器 |
| 62 | +func (m *minHeap) ScheduleFunc(expire time.Duration, callback func()) TimeNoder { |
| 63 | + return m.addCallback(expire, callback, true) |
| 64 | +} |
| 65 | + |
| 66 | +// 运行 |
| 67 | +// 为了避免空转cpu, 会等待一个chan, 只要AfterFunc或者ScheduleFunc被调用就会往这个chan里面写值 |
| 68 | +func (m *minHeap) Run() { |
| 69 | + |
| 70 | + tm := time.NewTimer(time.Hour) |
| 71 | + for { |
| 72 | + select { |
| 73 | + case <-tm.C: |
| 74 | + for { |
| 75 | + m.mu.Lock() |
| 76 | + now := time.Now() |
| 77 | + if m.minHeaps.Len() == 0 { |
| 78 | + m.mu.Unlock() |
| 79 | + goto next |
| 80 | + } |
| 81 | + |
| 82 | + first := &m.minHeaps[0] |
| 83 | + |
| 84 | + if now.After(first.absExpire) { |
| 85 | + callback := first.callback |
| 86 | + if first.isSchedule { |
| 87 | + first.absExpire = now.Add(first.userExpire) |
| 88 | + heap.Fix(&m.minHeaps, first.index) |
| 89 | + } else { |
| 90 | + m.minHeaps.Pop() |
| 91 | + } |
| 92 | + go callback() |
| 93 | + } |
| 94 | + |
| 95 | + if m.minHeaps.Len() == 0 { |
| 96 | + m.mu.Unlock() |
| 97 | + goto next |
| 98 | + } |
| 99 | + first = &m.minHeaps[0] |
| 100 | + if time.Now().Before(first.absExpire) { |
| 101 | + to := time.Duration(math.Abs(float64(time.Since(m.minHeaps[0].absExpire)))) |
| 102 | + tm.Reset(to) |
| 103 | + m.mu.Unlock() |
| 104 | + goto next |
| 105 | + } |
| 106 | + m.mu.Unlock() |
| 107 | + } |
| 108 | + case <-m.chAdd: |
| 109 | + m.mu.Lock() |
| 110 | + // 极端情况,加完任务立即给删除了, 判断下当前堆中是否有元素 |
| 111 | + if m.minHeaps.Len() > 0 { |
| 112 | + tm.Reset(time.Since(m.minHeaps[0].absExpire)) |
| 113 | + } |
| 114 | + m.mu.Unlock() |
| 115 | + // 进入事件循环,如果为空就会从事件循环里面退出 |
| 116 | + case <-m.ctx.Done(): |
| 117 | + // 等待所有任务结束 |
| 118 | + m.wait.Wait() |
| 119 | + return |
| 120 | + } |
| 121 | + next: |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// 停止所有定时器 |
| 126 | +func (m *minHeap) Stop() { |
| 127 | + m.cancel() |
| 128 | +} |
| 129 | + |
| 130 | +func newMinHeap() (mh *minHeap) { |
| 131 | + mh = &minHeap{} |
| 132 | + heap.Init(&mh.minHeaps) |
| 133 | + mh.chAdd = make(chan struct{}, 1) |
| 134 | + mh.ctx, mh.cancel = context.WithCancel(context.TODO()) |
| 135 | + return |
| 136 | +} |
0 commit comments