gtimer

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2023 License: MulanPSL-2.0 Imports: 7 Imported by: 4

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:

  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

Example (Add)
package main

import (
	"fmt"
	"time"

	"gitee.com/micro-tools/wf/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

Examples

Constants

View Source
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 DelayAdd

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

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

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

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

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

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

func SetInterval(interval time.Duration, job JobFunc)

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

func SetTimeout

func SetTimeout(delay time.Duration, job JobFunc)

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 Add

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

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

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

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

AddSingleton is a convenience function for add singleton mode job.

func AddTimes

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

func (entry *Entry) Close()

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

func (*Entry) IsSingleton

func (entry *Entry) IsSingleton() bool

IsSingleton checks and returns whether the job in singleton mode.

func (*Entry) Job

func (entry *Entry) Job() JobFunc

Job returns the job function of this job.

func (*Entry) Reset

func (entry *Entry) Reset()

Reset reset the job, which resets its ticks for next running.

func (*Entry) Run

func (entry *Entry) Run()

Run runs the timer job asynchronously.

func (*Entry) SetSingleton

func (entry *Entry) SetSingleton(enabled bool)

SetSingleton sets the job singleton mode.

func (*Entry) SetStatus

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

SetStatus custom sets the status for the job.

func (*Entry) SetTimes

func (entry *Entry) SetTimes(times int)

SetTimes sets the limit running times for the job.

func (*Entry) Start

func (entry *Entry) Start()

Start starts the job.

func (*Entry) Status

func (entry *Entry) Status() int

Status returns the status of the job.

func (*Entry) Stop

func (entry *Entry) Stop()

Stop stops the job.

type JobFunc

type JobFunc = func()

JobFunc is the job function.

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) Add

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

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

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

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

AddSingleton is a convenience function for add singleton mode job.

func (*Timer) AddTimes

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

func (t *Timer) Close()

Close closes the timer.

func (*Timer) DelayAdd

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

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

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

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

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

func (t *Timer) Start()

Start starts the timer.

func (*Timer) Stop

func (t *Timer) Stop()

Stop stops the timer.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL