timer

package module
v0.0.0-...-8bbe0b5 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2023 License: MIT Imports: 4 Imported by: 0

README

Timer

用于执行定时任务
需要执行n个定时任务时,根据任务启动时间排序,多个定时任务共用同一个定时器,不需要每个任务启一个定时器,减少goroutine资源

Example

package main

import (
	"github.com/zldongly/timer"
	"log"
	"time"
)

func main() {
	tr := timer.New()

	// 添加一个定时任务
	id := tr.Add(time.Now().Add(time.Second*10), func() {
		// TODO
	})

	// 重新定时
	err := tr.Set(id, time.Now().Add(time.Second))
	if err != nil {
		log.Fatalln(err)
	}

	// 删除定时任务
	tr.Del(id)
    
	// 循环启动定时任务
	loop(tr)
}

func loop(tr *timer.Timer) {
	tr.Add(time.Now().Add(time.Second), func() {
		// TODO

		loop(tr)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotExist = errors.New("timer: not exist task id")
)

Functions

This section is empty.

Types

type Option

type Option func(t *Timer)

Option 可选参数

func Size

func Size(size int) Option

type Task

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

type Timer

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

func New

func New(opts ...Option) *Timer

func (*Timer) Add

func (tr *Timer) Add(expire time.Time, fn func()) int64

Add 添加任务

func (*Timer) Del

func (tr *Timer) Del(id int64)

Del 按id删除任务

func (*Timer) Set

func (tr *Timer) Set(id int64, expire time.Time) error

Set 重置任务时间

Jump to

Keyboard shortcuts

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