TimingWheel
Golang实现的层级时间轮定时器
Install / Use
/learn @eliaszoo/TimingWheelREADME
Golang实现的层级时间轮定时器
TODO
- 使用小根堆优化ticker, 降低CPU唤醒频率
install
go get -u github.com/eliaszoo/TimingWheel
eg
package main
import (
"fmt"
"time"
"github.com/eliaszoo/TimingWheel"
)
func main() {
// 构造一个层级时间轮,第一层以1ms为一tick,共20个槽,第二层以1ms * 20为一tick,以此类推
tw, err := timing_wheel.NewTimingWheel(time.Millisecond, 20)
// 启动定时器
tw.Run()
// 停止定时器
defer tw.Stop()
durations := []int {50, 100, 111, 111, 112, 113, 200, 1000}
for _, d := range durations {
// 添加定时器, 第一个参数为延迟时间, 第二个参数为回调函数
tw.AfterFunc(time.Duration(d) * time.Millisecond, func() {
fmt.Println(time.Now().UnixNano() / int64(time.Millisecond))
})
}
select {}
}
Related Skills
node-connect
339.5kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
339.5kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
83.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.5kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
