timewheel

package module
v0.0.0-...-d8db05b Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2021 License: MIT Imports: 2 Imported by: 0

README

timewheel

Feature

  • Set tasks to be triggered by set time
  • TimeWheel one circle is one day
  • Automatically align the time every 1 hour

Usage

package main

import (
	"fmt"
	"time"
	
	"github.com/ClayCheung/timewheel"
)

func main()  {
	// initial timeWheel, set time goes interval and callback function
	tw := timewheel.New(1 * time.Second, func(data interface{}) {
		fmt.Println(data)
	})

	// Start timeWheel
	tw.Start()

	// Add timer
	// set trigger time(timestamp format)
	// set timer ID
	// set callback function's parameter
	tw.AddTimer(1623147042, "rule-1-start", "data-01"})
	tw.AddTimer(1623147102, "rule-1-end", "data-02")
	
	// Remove timer by ID
	tw.RemoveTimer("rule-1-start")
    
	// Stop TimeWheel
	//tw.Stop()

	select{}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Callback

type Callback func(interface{})

Callback 延时任务回调函数 TODO 搞成多参数?

type Task

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

Task 定时任务(是一个任务,可以创建定时器)

type TimeWheel

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

TimeWheel 时间轮,限定于一圈时间为1天的场景

func New

func New(interval time.Duration, callback Callback) *TimeWheel

New 创建时间轮

func (*TimeWheel) AddTimer

func (tw *TimeWheel) AddTimer(timestamp int64, key interface{}, data interface{})

AddTimer 添加定时器 key为定时器唯一标识,支持设置过去时间的任务

func (*TimeWheel) RemoveTimer

func (tw *TimeWheel) RemoveTimer(key interface{})

RemoveTimer 删除定时器 key为添加定时器时传递的定时器唯一标识

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start 启动时间轮

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop 停止时间轮

Jump to

Keyboard shortcuts

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