actor_timer.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package mhayaActor
  2. import (
  3. "time"
  4. mhayaTimeWheel "github.com/mhaya/extend/time_wheel"
  5. cutils "github.com/mhaya/extend/utils"
  6. clog "github.com/mhaya/logger"
  7. )
  8. const (
  9. updateTimerFuncName = "_updateTimer_"
  10. )
  11. type (
  12. actorTimer struct {
  13. thisActor *Actor
  14. timerInfoMap map[uint64]*timerInfo //key:timerId,value:*timerInfo
  15. }
  16. timerInfo struct {
  17. timer *mhayaTimeWheel.Timer
  18. fn func()
  19. once bool
  20. }
  21. )
  22. func newTimer(thisActor *Actor) actorTimer {
  23. return actorTimer{
  24. thisActor: thisActor,
  25. timerInfoMap: make(map[uint64]*timerInfo),
  26. }
  27. }
  28. func (p *actorTimer) onStop() {
  29. p.RemoveAll()
  30. p.thisActor = nil
  31. }
  32. func (p *actorTimer) Add(delay time.Duration, fn func(), async ...bool) uint64 {
  33. if delay.Milliseconds() < 1 || fn == nil {
  34. clog.Warnf("[ActorTimer] Add parameter error. delay = %+v", delay)
  35. return 0
  36. }
  37. newId := globalTimer.NextId()
  38. timer := globalTimer.AddEveryFunc(newId, delay, p.callUpdateTimer(newId), async...)
  39. if timer == nil {
  40. clog.Warnf("[ActorTimer] Add error. delay = %+v", delay)
  41. return 0
  42. }
  43. p.addTimerInfo(timer, fn, false)
  44. return newId
  45. }
  46. func (p *actorTimer) AddOnce(delay time.Duration, fn func(), async ...bool) uint64 {
  47. if delay.Milliseconds() < 1 || fn == nil {
  48. clog.Warnf("[ActorTimer] AddOnce parameter error. delay = %+v", delay)
  49. return 0
  50. }
  51. newId := globalTimer.NextId()
  52. timer := globalTimer.AfterFunc(newId, delay, p.callUpdateTimer(newId), async...)
  53. if timer == nil {
  54. clog.Warnf("[ActorTimer] AddOnce error. d = %+v", delay)
  55. return 0
  56. }
  57. p.addTimerInfo(timer, fn, true)
  58. return newId
  59. }
  60. func (p *actorTimer) AddFixedHour(hour, minute, second int, fn func(), async ...bool) uint64 {
  61. schedule := &mhayaTimeWheel.FixedDateSchedule{
  62. Hour: hour,
  63. Minute: minute,
  64. Second: second,
  65. }
  66. return p.AddSchedule(schedule, fn, async...)
  67. }
  68. func (p *actorTimer) AddFixedMinute(minute, second int, fn func(), async ...bool) uint64 {
  69. return p.AddFixedHour(-1, minute, second, fn, async...)
  70. }
  71. func (p *actorTimer) AddSchedule(s ITimerSchedule, fn func(), async ...bool) uint64 {
  72. if s == nil || fn == nil {
  73. return 0
  74. }
  75. newId := globalTimer.NextId()
  76. timer := globalTimer.ScheduleFunc(newId, s, p.callUpdateTimer(newId), async...)
  77. p.addTimerInfo(timer, fn, false)
  78. return newId
  79. }
  80. func (p *actorTimer) Remove(id uint64) {
  81. funcItem, found := p.timerInfoMap[id]
  82. if found {
  83. funcItem.timer.Stop()
  84. delete(p.timerInfoMap, id)
  85. }
  86. }
  87. func (p *actorTimer) RemoveAll() {
  88. for _, info := range p.timerInfoMap {
  89. info.timer.Stop()
  90. }
  91. }
  92. func (p *actorTimer) addTimerInfo(timer *mhayaTimeWheel.Timer, fn func(), once bool) {
  93. p.timerInfoMap[timer.ID()] = &timerInfo{
  94. timer: timer,
  95. fn: fn,
  96. once: once,
  97. }
  98. }
  99. func (p *actorTimer) callUpdateTimer(id uint64) func() {
  100. return func() {
  101. p.thisActor.Call(p.thisActor.PathString(), updateTimerFuncName, id)
  102. }
  103. }
  104. func (p *actorTimer) _updateTimer_(id uint64) {
  105. value, found := p.timerInfoMap[id]
  106. if !found {
  107. return
  108. }
  109. cutils.Try(func() {
  110. value.fn()
  111. }, func(errString string) {
  112. clog.Error(errString)
  113. })
  114. if value.once {
  115. delete(p.timerInfoMap, id)
  116. }
  117. }