Code Monkey home page Code Monkey logo

timewheel's Introduction

timewheel

Golang实现的时间轮

Go Report Card

时间轮

原理

延迟消息的实现

安装

go get -u github.com/ouqiang/timewheel

使用

package main

import (
    "github.com/ouqiang/timewheel"
    "time"
)

func main()  {
    // 初始化时间轮
    // 第一个参数为tick刻度, 即时间轮多久转动一次
    // 第二个参数为时间轮槽slot数量
    // 第三个参数为回调函数
    tw := timewheel.New(1 * time.Second, 3600, func(data interface{}) {
        // do something
    })
    
    // 启动时间轮
    tw.Start()
    
    // 添加定时器 
    // 第一个参数为延迟时间
    // 第二个参数为定时器唯一标识, 删除定时器需传递此参数
    // 第三个参数为用户自定义数据, 此参数将会传递给回调函数, 类型为interface{}
    tw.AddTimer(5 * time.Second, conn, map[string]int{"uid" : 105626})
    
    // 删除定时器, 参数为添加定时器传递的唯一标识
    tw.RemoveTimer(conn)
    
    // 停止时间轮
    tw.Stop()
    
    select{}
}

timewheel's People

Contributors

ouqiang avatar

Watchers

 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.