Timingwheel
分层时间轮的Golang实现
Install / Use
/learn @golearnku/TimingwheelREADME
timingwheel
分层时间轮的Golang实现, 基于RussellLuo/timingwheel 改造
Installation
$ go get -u github.com/golearnku/timingwheel
Design
timingwheel is ported from Kafka's purgatory, which is designed based on Hierarchical Timing Wheels.
中文博客:层级时间轮的 Golang 实现。
Documentation
For usage and examples see the Godoc.
AfterFunc
package main
import (
"fmt"
"time"
"github.com/golearnku/timingwheel"
)
func main() {
tw := timingwheel.NewTimingWheel(time.Millisecond, 20)
tw.Start()
defer tw.Stop()
t := tw.AfterFunc("100",time.Second, func() {
fmt.Println("The timer fires")
})
<-time.After(900 * time.Millisecond)
// Stop the timer before it fires
t.Stop()
}
package main
import (
"fmt"
"time"
"github.com/golearnku/timingwheel"
)
func main() {
tw := timingwheel.NewTimingWheel(time.Millisecond, 20)
tw.Start()
defer tw.Stop()
exitC := make(chan time.Time, 1)
tw.AfterFunc("100",time.Second * 2, func() {
fmt.Println("The timer fires")
exitC <- time.Now().UTC()
})
<-exitC
}
ScheduleFunc
package main
import (
"fmt"
"time"
"github.com/golearnku/timingwheel"
)
type EveryScheduler struct {
Id int
Interval time.Duration
}
func (s *EveryScheduler) Next(prev time.Time) time.Time {
return prev.Add(s.Interval)
}
func main() {
tw := timingwheel.NewTimingWheel(time.Millisecond, 20)
tw.Start()
defer tw.Stop()
i := 0
tw.ScheduleFunc("100", &EveryScheduler{1, time.Second * 2}, func() {
i++
fmt.Println("The timer fires")
fmt.Println(i)
//exitC <- time.Now().UTC()
})
for {
select {
case <-time.After(time.Millisecond * 300):
i += 100
case <-time.After(time.Second * 5):
tw.Remove("100")
return
}
}
}
Benchmark
$ go test -bench=. -benchmem
goos: darwin
goarch: amd64
pkg: github.com/golearnku/timingwheel
BenchmarkTimingWheel_StartStop/N-1m-8 2502430 459 ns/op 134 B/op 4 allocs/op
BenchmarkTimingWheel_StartStop/N-5m-8 2732517 522 ns/op 147 B/op 4 allocs/op
BenchmarkTimingWheel_StartStop/N-10m-8 2098280 493 ns/op 70 B/op 1 allocs/op
BenchmarkStandardTimer_StartStop/N-1m-8 7412431 232 ns/op 81 B/op 1 allocs/op
BenchmarkStandardTimer_StartStop/N-5m-8 4012328 290 ns/op 84 B/op 1 allocs/op
BenchmarkStandardTimer_StartStop/N-10m-8 5873055 280 ns/op 86 B/op 1 allocs/op
PASS
ok github.com/golearnku/timingwheel 80.234s
License
Related Skills
node-connect
339.3kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
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.3kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.9kCommit, push, and open a PR
