gtimer

package
v2.0.0-...-cbac34e Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package gtimer implements Hierarchical Timing Wheel for interval/delayed jobs running and management.

任务定时器, 高性能的分层时间轮任务管理模块,用于管理间隔/延迟运行任务。 与gcron模块的区别是,时间轮模块只管理间隔执行任务,并且更注重执行效率(纳秒级别)。 需要注意执行时间间隔的准确性问题: https://github.com/golang/go/issues/14410

Index

Examples

Constants

View Source
const (
	STATUS_READY   = 0
	STATUS_RUNNING = 1
	STATUS_STOPPED = 2
	STATUS_CLOSED  = -1
)

Variables

This section is empty.

Functions

func DelayAdd

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

延迟添加循环任务。

func DelayAddEntry

func DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)

延迟添加循环任务, 支持完整的参数。

func DelayAddOnce

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

延迟添加只运行一次的循环任务,delay参数单位为秒

func DelayAddSingleton

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

延迟添加单例循环任务,delay参数单位为秒

func DelayAddTimes

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

延迟添加运行指定次数的循环任务,delay参数单位为秒

func Exit

func Exit()

在Job方法中调用,停止并删除当前运行的任务。

func SetInterval

func SetInterval(interval time.Duration, job JobFunc)

类似与js中的SetInterval,每隔一段时间执行指定回调函数。

func SetTimeout

func SetTimeout(delay time.Duration, job JobFunc)

类似与js中的SetTimeout,一段时间后执行回调函数。

Types

type Entry

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

循环任务项

func Add

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

添加执行方法。

Example
package main

import (
	"fmt"
	"time"

	"github.com/gogf/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:

func AddEntry

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

添加执行方法,更多参数控制。

func AddOnce

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

添加只运行一次的循环任务。

func AddSingleton

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

添加单例运行循环任务。

func AddTimes

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

添加运行指定次数的循环任务。

func (*Entry) Close

func (entry *Entry) Close()

关闭当前任务

func (*Entry) IsSingleton

func (entry *Entry) IsSingleton() bool

是否单例运行

func (*Entry) Run

func (entry *Entry) Run()

执行任务

func (*Entry) SetSingleton

func (entry *Entry) SetSingleton(enabled bool)

设置单例运行

func (*Entry) SetStatus

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

设置任务状态

func (*Entry) SetTimes

func (entry *Entry) SetTimes(times int)

设置任务的运行次数

func (*Entry) Start

func (entry *Entry) Start()

启动当前任务

func (*Entry) Status

func (entry *Entry) Status() int

获取任务状态

func (*Entry) Stop

func (entry *Entry) Stop()

停止当前任务

type JobFunc

type JobFunc = func()

任务执行方法

type Timer

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

定时器/分层时间轮

func New

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

创建分层时间轮

func (*Timer) Add

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

添加循环任务

func (*Timer) AddEntry

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

添加定时任务

func (*Timer) AddOnce

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

添加只运行一次的循环任务

func (*Timer) AddSingleton

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

添加单例运行循环任务

func (*Timer) AddTimes

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

添加运行指定次数的循环任务。

func (*Timer) Close

func (t *Timer) Close()

关闭定时器

func (*Timer) DelayAdd

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

延迟添加循环任务。

func (*Timer) DelayAddEntry

func (t *Timer) DelayAddEntry(delay time.Duration, interval time.Duration, job JobFunc, singleton bool, times int, status int)

延迟添加循环任务, 支持完整的参数。

func (*Timer) DelayAddOnce

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

延迟添加只运行一次的循环任务

func (*Timer) DelayAddSingleton

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

延迟添加单例循环任务

func (*Timer) DelayAddTimes

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

延迟添加只运行一次的循环任务

func (*Timer) Start

func (t *Timer) Start()

启动定时器

func (*Timer) Stop

func (t *Timer) Stop()

定制定时器

Jump to

Keyboard shortcuts

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