github.com/zhongdalu/gf@v1.0.0/g/os/gtimer/gtimer_loop.go (about) 1 // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 package gtimer 8 9 import ( 10 "github.com/zhongdalu/gf/g/container/glist" 11 "time" 12 ) 13 14 // 开始循环 15 func (w *wheel) start() { 16 go func() { 17 ticker := time.NewTicker(time.Duration(w.intervalMs) * time.Millisecond) 18 for { 19 select { 20 case <-ticker.C: 21 switch w.timer.status.Val() { 22 case STATUS_RUNNING: 23 w.proceed() 24 25 case STATUS_STOPPED: 26 case STATUS_CLOSED: 27 ticker.Stop() 28 return 29 } 30 31 } 32 } 33 }() 34 } 35 36 // 执行时间轮刻度逻辑 37 func (w *wheel) proceed() { 38 n := w.ticks.Add(1) 39 l := w.slots[int(n%w.number)] 40 length := l.Len() 41 if length > 0 { 42 go func(l *glist.List, nowTicks int64) { 43 entry := (*Entry)(nil) 44 nowMs := time.Now().UnixNano() / 1e6 45 for i := length; i > 0; i-- { 46 if v := l.PopFront(); v == nil { 47 break 48 } else { 49 entry = v.(*Entry) 50 } 51 // 是否满足运行条件 52 runnable, addable := entry.check(nowTicks, nowMs) 53 if runnable { 54 // 异步执行运行 55 go func(entry *Entry) { 56 defer func() { 57 if err := recover(); err != nil { 58 if err != gPANIC_EXIT { 59 panic(err) 60 } else { 61 entry.Close() 62 } 63 } 64 if entry.Status() == STATUS_RUNNING { 65 entry.SetStatus(STATUS_READY) 66 } 67 }() 68 entry.job() 69 }(entry) 70 } 71 // 是否继续添运行, 滚动任务 72 if addable { 73 entry.wheel.timer.doAddEntryByParent(entry.rawIntervalMs, entry) 74 } 75 } 76 }(l, n) 77 } 78 }