gf/os/gtimer/gtimer.go

135 lines
5.5 KiB
Go
Raw Normal View History

2020-12-10 20:10:07 +08:00
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
2018-12-30 11:08:07 +08:00
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
2018-12-30 11:08:07 +08:00
2020-03-06 11:01:03 +08:00
// Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs
// running and management.
2019-06-19 09:06:52 +08:00
//
2020-03-06 11:01:03 +08:00
// This package is designed for management for millions of timing jobs. The differences
// between gtimer and gcron are as follows:
// 1. package gcron is implemented based on package gtimer.
2020-01-21 14:46:23 +08:00
// 2. gtimer is designed for high performance and for millions of timing jobs.
2020-03-30 20:31:47 +08:00
// 3. gcron supports configuration pattern grammar like linux crontab, which is more manually
// readable.
2020-03-06 11:01:03 +08:00
// 4. gtimer's benchmark OP is measured in nanoseconds, and gcron's benchmark OP is measured
// in microseconds.
2020-01-21 14:46:23 +08:00
//
2020-03-06 11:01:03 +08:00
// ALSO VERY NOTE the common delay of the timer: https://github.com/golang/go/issues/14410
package gtimer
2019-01-01 19:43:31 +08:00
2019-01-09 12:54:37 +08:00
import (
2020-07-12 09:34:43 +08:00
"fmt"
2019-06-19 09:06:52 +08:00
"math"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/os/gcmd"
2019-01-09 12:54:37 +08:00
)
2018-12-30 11:08:07 +08:00
const (
2020-01-21 14:46:23 +08:00
STATUS_READY = 0 // Job is ready for running.
STATUS_RUNNING = 1 // Job is already running.
STATUS_STOPPED = 2 // Job is stopped.
2020-07-29 11:22:46 +08:00
STATUS_RESET = 3 // Job is reset.
2020-01-21 14:46:23 +08:00
STATUS_CLOSED = -1 // Job is closed and waiting to be deleted.
gPANIC_EXIT = "exit" // Internal usage for custom job exit function with panic.
gDEFAULT_TIMES = math.MaxInt32 // Default limit running times, a big number.
gDEFAULT_SLOT_NUMBER = 10 // Default slot number.
2020-12-10 20:10:07 +08:00
gDEFAULT_WHEEL_INTERVAL = 100 // Default wheel interval.
gDEFAULT_WHEEL_LEVEL = 5 // Default wheel level.
2020-07-12 09:34:43 +08:00
gCMDENV_KEY = "gf.gtimer" // Configuration key for command argument or environment.
2018-12-30 11:08:07 +08:00
)
var (
defaultSlots = gcmd.GetWithEnv(fmt.Sprintf("%s.slots", gCMDENV_KEY), gDEFAULT_SLOT_NUMBER).Int()
defaultLevel = gcmd.GetWithEnv(fmt.Sprintf("%s.level", gCMDENV_KEY), gDEFAULT_WHEEL_LEVEL).Int()
defaultInterval = gcmd.GetWithEnv(fmt.Sprintf("%s.interval", gCMDENV_KEY), gDEFAULT_WHEEL_INTERVAL).Duration() * time.Millisecond
2020-01-21 14:46:23 +08:00
defaultTimer = New(defaultSlots, defaultInterval, defaultLevel)
2018-12-30 11:08:07 +08:00
)
2020-01-21 14:46:23 +08:00
// SetTimeout runs the job once after duration of <delay>.
// It is like the one in javascript.
func SetTimeout(delay time.Duration, job JobFunc) {
2019-06-19 09:06:52 +08:00
AddOnce(delay, job)
}
2020-01-21 14:46:23 +08:00
// SetInterval runs the job every duration of <delay>.
// It is like the one in javascript.
func SetInterval(interval time.Duration, job JobFunc) {
2019-06-19 09:06:52 +08:00
Add(interval, job)
}
2020-01-21 14:46:23 +08:00
// Add adds a timing job to the default timer, which runs in interval of <interval>.
2019-01-09 13:26:59 +08:00
func Add(interval time.Duration, job JobFunc) *Entry {
2019-06-19 09:06:52 +08:00
return defaultTimer.Add(interval, job)
2018-12-30 11:08:07 +08:00
}
2020-01-21 14:46:23 +08:00
// AddEntry adds a timing job to the default timer with detailed parameters.
//
// The parameter <interval> specifies the running interval of the job.
//
// The parameter <singleton> specifies whether the job running in singleton mode.
// There's only one of the same job is allowed running when its a singleton mode job.
//
// The parameter <times> specifies limit for the job running times, which means the job
// exits if its run times exceeds the <times>.
//
// The parameter <status> specifies the job status when it's firstly added to the timer.
func AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry {
2019-06-19 09:06:52 +08:00
return defaultTimer.AddEntry(interval, job, singleton, times, status)
2019-01-21 22:09:51 +08:00
}
2020-01-21 14:46:23 +08:00
// AddSingleton is a convenience function for add singleton mode job.
2019-01-09 13:26:59 +08:00
func AddSingleton(interval time.Duration, job JobFunc) *Entry {
2019-06-19 09:06:52 +08:00
return defaultTimer.AddSingleton(interval, job)
2018-12-30 11:08:07 +08:00
}
2020-01-21 14:46:23 +08:00
// AddOnce is a convenience function for adding a job which only runs once and then exits.
2019-01-09 13:26:59 +08:00
func AddOnce(interval time.Duration, job JobFunc) *Entry {
2019-06-19 09:06:52 +08:00
return defaultTimer.AddOnce(interval, job)
2019-01-01 19:43:31 +08:00
}
2020-01-21 14:46:23 +08:00
// AddTimes is a convenience function for adding a job which is limited running times.
2019-01-09 13:26:59 +08:00
func AddTimes(interval time.Duration, times int, job JobFunc) *Entry {
2019-06-19 09:06:52 +08:00
return defaultTimer.AddTimes(interval, times, job)
2018-12-30 11:08:07 +08:00
}
2020-01-21 14:46:23 +08:00
// DelayAdd adds a timing job after delay of <interval> duration.
// Also see Add.
2019-01-03 19:11:54 +08:00
func DelayAdd(delay time.Duration, interval time.Duration, job JobFunc) {
2019-06-19 09:06:52 +08:00
defaultTimer.DelayAdd(delay, interval, job)
2018-12-30 11:08:07 +08:00
}
2020-01-21 14:46:23 +08:00
// DelayAddEntry adds a timing job after delay of <interval> duration.
// Also see AddEntry.
func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int) {
2019-06-19 09:06:52 +08:00
defaultTimer.DelayAddEntry(delay, interval, job, singleton, times, status)
2019-01-21 22:09:51 +08:00
}
2020-01-21 14:46:23 +08:00
// DelayAddSingleton adds a timing job after delay of <interval> duration.
// Also see AddSingleton.
2019-01-03 19:11:54 +08:00
func DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc) {
2019-06-19 09:06:52 +08:00
defaultTimer.DelayAddSingleton(delay, interval, job)
2018-12-30 11:08:07 +08:00
}
2020-01-21 14:46:23 +08:00
// DelayAddOnce adds a timing job after delay of <interval> duration.
// Also see AddOnce.
2019-01-03 19:11:54 +08:00
func DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc) {
2019-06-19 09:06:52 +08:00
defaultTimer.DelayAddOnce(delay, interval, job)
2019-01-01 19:43:31 +08:00
}
2020-01-21 14:46:23 +08:00
// DelayAddTimes adds a timing job after delay of <interval> duration.
// Also see AddTimes.
2019-01-03 19:11:54 +08:00
func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc) {
2019-06-19 09:06:52 +08:00
defaultTimer.DelayAddTimes(delay, interval, times, job)
}
2020-03-06 11:01:03 +08:00
// Exit is used in timing job internally, which exits and marks it closed from timer.
// The timing job will be automatically removed from timer later. It uses "panic-recover"
// mechanism internally implementing this feature, which is designed for simplification
// and convenience.
2019-01-02 10:18:00 +08:00
func Exit() {
2019-06-19 09:06:52 +08:00
panic(gPANIC_EXIT)
2018-12-30 14:53:16 +08:00
}