timewheel

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 6 Imported by: 2

README

timewheel

Pure golang implementation for timewheel.

Go Report Card Go codecov Releases Godoc Go Reference LICENSE

pic/timewheel.png

Usage

Installing

To start using timewheel, install Go and run go get:

$ go get github.com/jhunters/timewheel
base method

create timewheel

// 初始化时间轮
// 第一个参数为tick刻度, 即时间轮多久转动一次
// 第二个参数为时间轮槽slot数量
tw, err := timewheel.New(100*time.Millisecond, 300)
if err != nil {
    panic(err)
}

tw.Start()

add delay task

// create a task bind with key, data and  time out call back function.
t := &timewheel.Task{
    Data: map[string]int{"uid": 105626, "age": 100}, // business data
    TimeoutCallback: func(task timewheel.Task) { // call back function on time out
        // process someting after time out happened. 
        fmt.Println("time out:", task.Delay(), task.Key, task.Data, task.Elasped())
    }}

// add task and return unique task id
taskid, err := tw.AddTask(5*time.Second, *t) // add delay task

remove delay task

tw.Remove(taskid)

check task

tw.HasTask(taskid)

close time wheel

tw.Stop()

example

example/demo.go

Documentation

Overview

Copyright 2021 The baidu Authors

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Task

type Task[E any] struct {
	Data            E
	TimeoutCallback TimeoutCallbackFn[E]
	// contains filtered or unexported fields
}

Task task struct

func (*Task[E]) Delay

func (task *Task[E]) Delay() time.Duration

Delay return delay time

func (*Task[E]) Elasped

func (t *Task[E]) Elasped() time.Duration

Elasped to get task

type TaskSlot

type TaskSlot[E any] struct {
	// contains filtered or unexported fields
}

TaskSlot a task with target slot info

type TimeWheel

type TimeWheel[E any] struct {
	// contains filtered or unexported fields
}

TimeWheel 时间轮

Example

ExampleTimeWheel example code for simple timewheel api usage.

package main

import (
	"fmt"
	"time"

	"github.com/jhunters/timewheel"
)

func main() {
	// 初始化时间轮
	// 第一个参数为tick刻度, 即时间轮多久转动一次
	// 第二个参数为时间轮槽slot数量
	tw, err := timewheel.New[map[string]int](100*time.Millisecond, 300)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("time wheel created.")

	// 启动时间轮
	tw.Start()
	fmt.Println("time wheel started.")

	t := &timewheel.Task[map[string]int]{
		Data: map[string]int{"uid": 105626, "age": 100}, // call back data
		TimeoutCallback: func(task timewheel.Task[map[string]int]) { // call back function on time out
			data := task.Data
			fmt.Println("time out:", task.Delay(), data["uid"], data["age"] /*, task.Elasped()*/)
		}}

	// add task and return unique task id
	taskid, err := tw.AddTask(5*time.Second, *t)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("add a new task. taskid=", taskid)

	// before time out we remove the task
	tw.RemoveTask(taskid)
	fmt.Println("remove task. taskid=", taskid)

	// add a new task again
	taskid, err = tw.AddTask(5*time.Second, *t)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("add a new task.  taskid=", taskid)

	fmt.Println("wait 10 seconds here.")
	time.Sleep(10 * time.Second)

	// 删除定时器, 参数为添加定时器传递的唯一标识
	tw.RemoveTask(taskid)

	// 停止时间轮
	tw.Stop()

}
Output:

func New

func New[E any](interval time.Duration, slotNum uint16) (*TimeWheel[E], error)

New 创建时间轮

func (*TimeWheel[E]) AddTask

func (tw *TimeWheel[E]) AddTask(delay time.Duration, task Task[E]) (taskid, error)

AddTimer 添加定时器 key为定时器唯一标识

func (*TimeWheel[E]) HasTask

func (tw *TimeWheel[E]) HasTask(key taskid) bool

HasTask to check task id exist

func (*TimeWheel[E]) RemoveTask

func (tw *TimeWheel[E]) RemoveTask(key taskid)

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

func (*TimeWheel[E]) Start

func (tw *TimeWheel[E]) Start()

Start 启动时间轮

func (*TimeWheel[E]) Stop

func (tw *TimeWheel[E]) Stop()

Stop 停止时间轮

type TimeoutCallbackFn

type TimeoutCallbackFn[E any] func(Task[E])

超时任务回调函数

Jump to

Keyboard shortcuts

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