Code Monkey home page Code Monkey logo

timewheel's Introduction

timewheel

Golang实现的时间轮

Go Report Card

时间轮

原理

延迟消息的实现

安装

go get -u github.com/dlwm/timewheel

使用

package main

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

func main() {
	// 初始化时间轮
	// 第一个参数为tick刻度,即时间轮多久转动一次,最小为1秒,跨度1秒
	// 第二个参数为时间轮槽slot数量,即每轮有多少个记录点
	// 第三个参数为回调函数
	tw := timewheel.New(1*time.Second, 3600, func(i interface{}) error {
		fmt.Println(i)
		return nil
	})

	// 开启时间轮
	tw.Start()

	// 添加定时器 
	// 第一个参数为周期性开启标记
	// 第二个参数为延迟时间
	// 第三个参数为定时器唯一标识,删除定时器需传递此参数
	// 第四个参数为用户自定义数据,此参数将会传递给回调函数, 类型为interface{}
	tw.AddTimer(true, 3*time.Second, 9028, "LOOP")  // 周期性任务
	tw.AddTimer(false, 5*time.Second, 9376, "NOOP") // 单次任务
	
	// 根据索引删除任务,参数为添加定时器传递的唯一标识
	tw.RemoveTimer(9028)
	
	// 暂停时间轮
	tw.Stop()

	// 继续时间轮
	tw.Start()

	select {}
}

timewheel's People

Contributors

dlwm avatar ouqiang avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.