gtimer.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf.
  6. // Package gtimer implements timer for interval/delayed jobs running and management.
  7. //
  8. // This package is designed for management for millions of timing jobs. The differences
  9. // between gtimer and gcron are as follows:
  10. // 1. package gcron is implemented based on package gtimer.
  11. // 2. gtimer is designed for high performance and for millions of timing jobs.
  12. // 3. gcron supports configuration pattern grammar like linux crontab, which is more manually
  13. // readable.
  14. // 4. gtimer's benchmark OP is measured in nanoseconds, and gcron's benchmark OP is measured
  15. // in microseconds.
  16. //
  17. // ALSO VERY NOTE the common delay of the timer: https://github.com/golang/go/issues/14410
  18. package gtimer
  19. import (
  20. "context"
  21. "strconv"
  22. "sync"
  23. "time"
  24. "github.com/gogf/gf/v2/container/gtype"
  25. "github.com/gogf/gf/v2/errors/gcode"
  26. "github.com/gogf/gf/v2/errors/gerror"
  27. "github.com/gogf/gf/v2/internal/command"
  28. )
  29. // Timer is the timer manager, which uses ticks to calculate the timing interval.
  30. type Timer struct {
  31. mu sync.RWMutex
  32. queue *priorityQueue // queue is a priority queue based on heap structure.
  33. status *gtype.Int // status is the current timer status.
  34. ticks *gtype.Int64 // ticks is the proceeded interval number by the timer.
  35. options TimerOptions // timer options is used for timer configuration.
  36. }
  37. // TimerOptions is the configuration object for Timer.
  38. type TimerOptions struct {
  39. Interval time.Duration // Interval is the interval escaped of the timer.
  40. }
  41. // internalPanic is the custom panic for internal usage.
  42. type internalPanic string
  43. const (
  44. StatusReady = 0 // Job or Timer is ready for running.
  45. StatusRunning = 1 // Job or Timer is already running.
  46. StatusStopped = 2 // Job or Timer is stopped.
  47. StatusClosed = -1 // Job or Timer is closed and waiting to be deleted.
  48. panicExit internalPanic = "exit" // panicExit is used for custom job exit with panic.
  49. defaultTimerInterval = "100" // defaultTimerInterval is the default timer interval in milliseconds.
  50. // commandEnvKeyForInterval is the key for command argument or environment configuring default interval duration for timer.
  51. commandEnvKeyForInterval = "gf.gtimer.interval"
  52. )
  53. var (
  54. defaultInterval = getDefaultInterval()
  55. defaultTimer = New()
  56. )
  57. func getDefaultInterval() time.Duration {
  58. interval := command.GetOptWithEnv(commandEnvKeyForInterval, defaultTimerInterval)
  59. n, err := strconv.Atoi(interval)
  60. if err != nil {
  61. panic(gerror.WrapCodef(
  62. gcode.CodeInvalidConfiguration, err, `error converting string "%s" to int number`,
  63. interval,
  64. ))
  65. }
  66. return time.Duration(n) * time.Millisecond
  67. }
  68. // DefaultOptions creates and returns a default options object for Timer creation.
  69. func DefaultOptions() TimerOptions {
  70. return TimerOptions{
  71. Interval: defaultInterval,
  72. }
  73. }
  74. // SetTimeout runs the job once after duration of `delay`.
  75. // It is like the one in javascript.
  76. func SetTimeout(ctx context.Context, delay time.Duration, job JobFunc) {
  77. AddOnce(ctx, delay, job)
  78. }
  79. // SetInterval runs the job every duration of `delay`.
  80. // It is like the one in javascript.
  81. func SetInterval(ctx context.Context, interval time.Duration, job JobFunc) {
  82. Add(ctx, interval, job)
  83. }
  84. // Add adds a timing job to the default timer, which runs in interval of `interval`.
  85. func Add(ctx context.Context, interval time.Duration, job JobFunc) *Entry {
  86. return defaultTimer.Add(ctx, interval, job)
  87. }
  88. // AddEntry adds a timing job to the default timer with detailed parameters.
  89. //
  90. // The parameter `interval` specifies the running interval of the job.
  91. //
  92. // The parameter `singleton` specifies whether the job running in singleton mode.
  93. // There's only one of the same job is allowed running when its a singleton mode job.
  94. //
  95. // The parameter `times` specifies limit for the job running times, which means the job
  96. // exits if its run times exceeds the `times`.
  97. //
  98. // The parameter `status` specifies the job status when it's firstly added to the timer.
  99. func AddEntry(ctx context.Context, interval time.Duration, job JobFunc, isSingleton bool, times int, status int) *Entry {
  100. return defaultTimer.AddEntry(ctx, interval, job, isSingleton, times, status)
  101. }
  102. // AddSingleton is a convenience function for add singleton mode job.
  103. func AddSingleton(ctx context.Context, interval time.Duration, job JobFunc) *Entry {
  104. return defaultTimer.AddSingleton(ctx, interval, job)
  105. }
  106. // AddOnce is a convenience function for adding a job which only runs once and then exits.
  107. func AddOnce(ctx context.Context, interval time.Duration, job JobFunc) *Entry {
  108. return defaultTimer.AddOnce(ctx, interval, job)
  109. }
  110. // AddTimes is a convenience function for adding a job which is limited running times.
  111. func AddTimes(ctx context.Context, interval time.Duration, times int, job JobFunc) *Entry {
  112. return defaultTimer.AddTimes(ctx, interval, times, job)
  113. }
  114. // DelayAdd adds a timing job after delay of `interval` duration.
  115. // Also see Add.
  116. func DelayAdd(ctx context.Context, delay time.Duration, interval time.Duration, job JobFunc) {
  117. defaultTimer.DelayAdd(ctx, delay, interval, job)
  118. }
  119. // DelayAddEntry adds a timing job after delay of `interval` duration.
  120. // Also see AddEntry.
  121. func DelayAddEntry(ctx context.Context, delay time.Duration, interval time.Duration, job JobFunc, isSingleton bool, times int, status int) {
  122. defaultTimer.DelayAddEntry(ctx, delay, interval, job, isSingleton, times, status)
  123. }
  124. // DelayAddSingleton adds a timing job after delay of `interval` duration.
  125. // Also see AddSingleton.
  126. func DelayAddSingleton(ctx context.Context, delay time.Duration, interval time.Duration, job JobFunc) {
  127. defaultTimer.DelayAddSingleton(ctx, delay, interval, job)
  128. }
  129. // DelayAddOnce adds a timing job after delay of `interval` duration.
  130. // Also see AddOnce.
  131. func DelayAddOnce(ctx context.Context, delay time.Duration, interval time.Duration, job JobFunc) {
  132. defaultTimer.DelayAddOnce(ctx, delay, interval, job)
  133. }
  134. // DelayAddTimes adds a timing job after delay of `interval` duration.
  135. // Also see AddTimes.
  136. func DelayAddTimes(ctx context.Context, delay time.Duration, interval time.Duration, times int, job JobFunc) {
  137. defaultTimer.DelayAddTimes(ctx, delay, interval, times, job)
  138. }