Timers represent a single event in the future. You tell the timer how long you want to wait, and You provide call back with time to laps. After that time your function will be called.

Here i am showing you how to manage a million of timers with help of Tiniyo timer it internally uses timer wheel to manage timer. So this timer is single threaded and manage millions of timers.

Following is code sample.

package main

import (
	"math/rand"
	"sync"

	timer "github.com/tiniyo/timer"
)

var testTimer timer.TiniyoTimer

type testTimerData struct {
	message string
}

var wg sync.WaitGroup

func testTimerCallbackHandler(data interface{}) error {
	//fmt.Println("testTimerCallbackHandler : ", data.(testTimerData).message)
	wg.Done()
	return nil
}

func main() {
	testTimer.InitializeTiniyoTimer(testTimerCallbackHandler)
	testTimer.Run()
	min := 1
	max := 5
	wg.Add(1)
	for i := 0; i < 1000000; i++ {
		testTimer.StartTimer(uint64(rand.Intn(max-min)+min), string(i), testTimerData{message: "This is test"})
		wg.Add(1)
	}
	wg.Wait()
}

Above code sample creates 1million timers in a range of 1-5 seconds. and it wait for it to expire. Once all the timers expire, main exits.1MN Timers in Golang