gf/os/gtimer/gtimer.go

127 lines
5.1 KiB
Go
Raw Normal View History

// Copyright 2019 gf 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
2019-01-22 13:50:10 +08:00
// Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.
2019-06-19 09:06:52 +08:00
//
2020-01-21 14:46:23 +08:00
// This package is designed for management for millions of timing jobs.
// The differences between gtime and gcron are as follows:
// 1. gcron is implemented based on gtimer.
// 2. gtimer is designed for high performance and for millions of timing jobs.
// 3. gcron supports pattern grammar like linux crontab.
// 4. gtimer's benchmark OP is measured in nanoseconds, and gcron's benchmark OP is measured in microseconds.
//
// 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 (
2019-06-19 09:06:52 +08:00
"math"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/internal/cmdenv"
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.
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.
gDEFAULT_WHEEL_INTERVAL = 50 // Default wheel interval.
gDEFAULT_WHEEL_LEVEL = 6 // Default wheel level.
2018-12-30 11:08:07 +08:00
)
var (
2019-06-19 09:06:52 +08:00
defaultSlots = cmdenv.Get("gf.gtimer.slots", gDEFAULT_SLOT_NUMBER).Int()
defaultLevel = cmdenv.Get("gf.gtimer.level", gDEFAULT_WHEEL_LEVEL).Int()
defaultInterval = cmdenv.Get("gf.gtimer.interval", 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-01-21 14:46:23 +08:00
// Exit is used in timing job, which exits and marks it closed from timer.
// The timing job will be removed from timer later.
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
}