Documentation ¶
Overview ¶
Package gtimer implements timer for interval/delayed jobs running and management.
This package is designed for management for millions of timing jobs. The differences between gtimer and gcron are as follows:
- package gcron is implemented based on package gtimer.
- gtimer is designed for high performance and for millions of timing jobs.
- gcron supports configuration pattern grammar like linux crontab, which is more manually readable.
- gtimer's benchmark OP is measured in nanoseconds, and gcron's benchmark OP is measured in microseconds.
ALSO VERY NOTE the common delay of the timer: https://github.com/golang/go/issues/14410
Example (Add) ¶
package main import ( "fmt" "time" "github.com/rglujing/gf/os/gtimer" ) func main() { now := time.Now() interval := 1400 * time.Millisecond gtimer.Add(interval, func() { fmt.Println(time.Now(), time.Duration(time.Now().UnixNano()-now.UnixNano())) now = time.Now() }) select {} }
Output:
Index ¶
- Constants
- func DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, ...)
- func DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)
- func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)
- func Exit()
- func SetInterval(interval time.Duration, job JobFunc)
- func SetTimeout(delay time.Duration, job JobFunc)
- type Entry
- func Add(interval time.Duration, job JobFunc) *Entry
- func AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
- func AddOnce(interval time.Duration, job JobFunc) *Entry
- func AddSingleton(interval time.Duration, job JobFunc) *Entry
- func AddTimes(interval time.Duration, times int, job JobFunc) *Entry
- func (entry *Entry) Close()
- func (entry *Entry) IsSingleton() bool
- func (entry *Entry) Job() JobFunc
- func (entry *Entry) Reset()
- func (entry *Entry) Run()
- func (entry *Entry) SetSingleton(enabled bool)
- func (entry *Entry) SetStatus(status int) int
- func (entry *Entry) SetTimes(times int)
- func (entry *Entry) Start()
- func (entry *Entry) Status() int
- func (entry *Entry) Stop()
- type JobFunc
- type Timer
- func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
- func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry
- func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry
- func (t *Timer) Close()
- func (t *Timer) DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, ...)
- func (t *Timer) DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)
- func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)
- func (t *Timer) Start()
- func (t *Timer) Stop()
- type TimerOptions
Examples ¶
Constants ¶
const ( StatusReady = 0 // Job or Timer is ready for running. StatusRunning = 1 // Job or Timer is already running. StatusStopped = 2 // Job or Timer is stopped. StatusClosed = -1 // Job or Timer is closed and waiting to be deleted. )
Variables ¶
This section is empty.
Functions ¶
func DelayAddEntry ¶
func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)
DelayAddEntry adds a timing job after delay of <interval> duration. Also see AddEntry.
func DelayAddOnce ¶
DelayAddOnce adds a timing job after delay of <interval> duration. Also see AddOnce.
func DelayAddSingleton ¶
DelayAddSingleton adds a timing job after delay of <interval> duration. Also see AddSingleton.
func DelayAddTimes ¶
DelayAddTimes adds a timing job after delay of <interval> duration. Also see AddTimes.
func Exit ¶
func Exit()
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.
func SetInterval ¶
SetInterval runs the job every duration of <delay>. It is like the one in javascript.
func SetTimeout ¶
SetTimeout runs the job once after duration of <delay>. It is like the one in javascript.
Types ¶
type Entry ¶
type Entry struct {
// contains filtered or unexported fields
}
Entry is the timing job.
func AddEntry ¶
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 AddOnce ¶
AddOnce is a convenience function for adding a job which only runs once and then exits.
func AddSingleton ¶
AddSingleton is a convenience function for add singleton mode job.
func (*Entry) Close ¶
func (entry *Entry) Close()
Close closes the job, and then it will be removed from the timer.
func (*Entry) IsSingleton ¶
IsSingleton checks and returns whether the job in singleton mode.
func (*Entry) Reset ¶
func (entry *Entry) Reset()
Reset resets the job, which resets its ticks for next running.
func (*Entry) SetSingleton ¶
SetSingleton sets the job singleton mode.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer is the timer manager, which uses ticks to calculate the timing interval.
func New ¶
func New(options ...TimerOptions) *Timer
func (*Timer) AddEntry ¶
func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry
AddEntry adds a timing job to the 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 (*Timer) AddOnce ¶
AddOnce is a convenience function for adding a job which only runs once and then exits.
func (*Timer) AddSingleton ¶
AddSingleton is a convenience function for add singleton mode job.
func (*Timer) AddTimes ¶
AddTimes is a convenience function for adding a job which is limited running times.
func (*Timer) DelayAdd ¶
DelayAdd adds a timing job after delay of <interval> duration. Also see Add.
func (*Timer) DelayAddEntry ¶
func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)
DelayAddEntry adds a timing job after delay of <interval> duration. Also see AddEntry.
func (*Timer) DelayAddOnce ¶
DelayAddOnce adds a timing job after delay of <interval> duration. Also see AddOnce.
func (*Timer) DelayAddSingleton ¶
DelayAddSingleton adds a timing job after delay of <interval> duration. Also see AddSingleton.
func (*Timer) DelayAddTimes ¶
DelayAddTimes adds a timing job after delay of <interval> duration. Also see AddTimes.
type TimerOptions ¶
type TimerOptions struct {
Interval time.Duration // Interval is the interval escaped of the timer.
}
TimerOptions is the configuration object for Timer.
func DefaultOptions ¶
func DefaultOptions() TimerOptions
DefaultOptions creates and returns a default options object for Timer creation.