gf: github.com/gogf/gf/os/gtimer Index | Examples | Files

package gtimer

import "github.com/gogf/gf/os/gtimer"

Package gtimer implements Hierarchical Timing Wheel 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: 1. package gcron is implemented based on package gtimer. 2. gtimer is designed for high performance and for millions of timing jobs. 3. gcron supports configuration pattern grammar like linux crontab, which is more manually

readable.

4. 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

Code:

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 {}

Index

Examples

Package Files

gtimer.go gtimer_entry.go gtimer_loop.go gtimer_timer.go

Constants

const (
    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.

)

func DelayAdd Uses

func DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)

DelayAdd adds a timing job after delay of <interval> duration. Also see Add.

func DelayAddEntry Uses

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 Uses

func DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)

DelayAddOnce adds a timing job after delay of <interval> duration. Also see AddOnce.

func DelayAddSingleton Uses

func DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)

DelayAddSingleton adds a timing job after delay of <interval> duration. Also see AddSingleton.

func DelayAddTimes Uses

func DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)

DelayAddTimes adds a timing job after delay of <interval> duration. Also see AddTimes.

func Exit Uses

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 Uses

func SetInterval(interval time.Duration, job JobFunc)

SetInterval runs the job every duration of <delay>. It is like the one in javascript.

func SetTimeout Uses

func SetTimeout(delay time.Duration, job JobFunc)

SetTimeout runs the job once after duration of <delay>. It is like the one in javascript.

type Entry Uses

type Entry struct {
    // contains filtered or unexported fields
}

Entry is the timing job entry to wheel.

func Add Uses

func Add(interval time.Duration, job JobFunc) *Entry

Add adds a timing job to the default timer, which runs in interval of <interval>.

func AddEntry Uses

func AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry

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 Uses

func AddOnce(interval time.Duration, job JobFunc) *Entry

AddOnce is a convenience function for adding a job which only runs once and then exits.

func AddSingleton Uses

func AddSingleton(interval time.Duration, job JobFunc) *Entry

AddSingleton is a convenience function for add singleton mode job.

func AddTimes Uses

func AddTimes(interval time.Duration, times int, job JobFunc) *Entry

AddTimes is a convenience function for adding a job which is limited running times.

func (*Entry) Close Uses

func (entry *Entry) Close()

Close closes the job, and then it will be removed from the timer.

func (*Entry) IsSingleton Uses

func (entry *Entry) IsSingleton() bool

IsSingleton checks and returns whether the job in singleton mode.

func (*Entry) Run Uses

func (entry *Entry) Run()

Run runs the job.

func (*Entry) SetSingleton Uses

func (entry *Entry) SetSingleton(enabled bool)

SetSingleton sets the job singleton mode.

func (*Entry) SetStatus Uses

func (entry *Entry) SetStatus(status int) int

SetStatus custom sets the status for the job.

func (*Entry) SetTimes Uses

func (entry *Entry) SetTimes(times int)

SetTimes sets the limit running times for the job.

func (*Entry) Start Uses

func (entry *Entry) Start()

Start starts the job.

func (*Entry) Status Uses

func (entry *Entry) Status() int

Status returns the status of the job.

func (*Entry) Stop Uses

func (entry *Entry) Stop()

Stop stops the job.

type JobFunc Uses

type JobFunc = func()

JobFunc is the job function.

type Timer Uses

type Timer struct {
    // contains filtered or unexported fields
}

Timer is a Hierarchical Timing Wheel manager for timing jobs.

func New Uses

func New(slot int, interval time.Duration, level ...int) *Timer

New creates and returns a Hierarchical Timing Wheel designed timer. The parameter <interval> specifies the interval of the timer. The optional parameter <level> specifies the wheels count of the timer, which is gDEFAULT_WHEEL_LEVEL in default.

func (*Timer) Add Uses

func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry

Add adds a timing job to the timer, which runs in interval of <interval>.

func (*Timer) AddEntry Uses

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 Uses

func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry

AddOnce is a convenience function for adding a job which only runs once and then exits.

func (*Timer) AddSingleton Uses

func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry

AddSingleton is a convenience function for add singleton mode job.

func (*Timer) AddTimes Uses

func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry

AddTimes is a convenience function for adding a job which is limited running times.

func (*Timer) Close Uses

func (t *Timer) Close()

Close closes the timer.

func (*Timer) DelayAdd Uses

func (t *Timer) DelayAdd(delay time.Duration, interval time.Duration, job JobFunc)

DelayAdd adds a timing job after delay of <interval> duration. Also see Add.

func (*Timer) DelayAddEntry Uses

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 Uses

func (t *Timer) DelayAddOnce(delay time.Duration, interval time.Duration, job JobFunc)

DelayAddOnce adds a timing job after delay of <interval> duration. Also see AddOnce.

func (*Timer) DelayAddSingleton Uses

func (t *Timer) DelayAddSingleton(delay time.Duration, interval time.Duration, job JobFunc)

DelayAddSingleton adds a timing job after delay of <interval> duration. Also see AddSingleton.

func (*Timer) DelayAddTimes Uses

func (t *Timer) DelayAddTimes(delay time.Duration, interval time.Duration, times int, job JobFunc)

DelayAddTimes adds a timing job after delay of <interval> duration. Also see AddTimes.

func (*Timer) Start Uses

func (t *Timer) Start()

Start starts the timer.

func (*Timer) Stop Uses

func (t *Timer) Stop()

Stop stops the timer.

Package gtimer imports 6 packages (graph) and is imported by 6 packages. Updated 2020-05-01. Refresh now. Tools for package owners.