GetTimerWithIn 获取定时器在一段时间间隔内的Timer
(duration time.Duration)
| 195 | |
| 196 | // GetTimerWithIn 获取定时器在一段时间间隔内的Timer |
| 197 | func (tw *TimeWheel) GetTimerWithIn(duration time.Duration) map[uint32]*Timer { |
| 198 | //最终触发定时器的一定是挂载最底层时间轮上的定时器 |
| 199 | //1 找到最底层时间轮 |
| 200 | leaftw := tw |
| 201 | for leaftw.nextTimeWheel != nil { |
| 202 | leaftw = leaftw.nextTimeWheel |
| 203 | } |
| 204 | |
| 205 | leaftw.Lock() |
| 206 | defer leaftw.Unlock() |
| 207 | //返回的Timer集合 |
| 208 | timerList := make(map[uint32]*Timer) |
| 209 | |
| 210 | now := UnixMilli() |
| 211 | |
| 212 | //取出当前时间轮刻度内全部Timer |
| 213 | for tID, timer := range leaftw.timerQueue[leaftw.curIndex] { |
| 214 | if timer.unixts-now < int64(duration/1e6) { |
| 215 | //当前定时器已经超时 |
| 216 | timerList[tID] = timer |
| 217 | //定时器已经超时被取走,从当前时间轮上 摘除该定时器 |
| 218 | delete(leaftw.timerQueue[leaftw.curIndex], tID) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return timerList |
| 223 | } |