github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/timingwheel/example_scheduler_test.go (about) 1 package timingwheel 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 type EveryScheduler struct { 9 Interval time.Duration 10 } 11 12 func (s *EveryScheduler) Next(prev time.Time) time.Time { 13 return prev.Add(s.Interval) 14 } 15 16 func Example_scheduleTimer() { 17 tw := NewTimingWheel(time.Millisecond, 20) 18 tw.Start() 19 defer tw.Stop() 20 21 exitC := make(chan time.Time) 22 t := tw.ScheduleFunc(&EveryScheduler{3*time.Second}, func() { 23 fmt.Println("The timer fires") 24 exitC <- time.Now().UTC() 25 }) 26 27 <-exitC 28 <-exitC 29 30 // We need to stop the timer since it will be restarted again and again. 31 for !t.Stop() { 32 } 33 34 // Output: 35 // The timer fires 36 // The timer fires 37 }