Skip to content

andy2046/tik

Repository files navigation

tik

Documentation GitHub issues license Release


hierarchical timing wheel made easy

simplified version of timeout in Golang

for documentation, view the API reference

Install

go get github.com/andy2046/tik

Usage

package main

import (
	"sync"
	"time"

	"github.com/andy2046/tik"
)

func main() {
	var l sync.RWMutex
	// init a new instance
	tk := tik.New()
	i := 0
	cb := func() {
		l.Lock()
		i++
		l.Unlock()
	}
	// schedule to run cb in 500ms
	to := tk.Schedule(500, cb)

	if !to.Pending() {
		panic("it should be pending")
	}

	if to.Expired() {
		panic("it should NOT be expired")
	}

	for {
		time.Sleep(100 * time.Millisecond)

		if tk.AnyPending() {
			continue
		}

		if tk.AnyExpired() {
			continue
		}

		break
	}

	l.RLock()
	defer l.RUnlock()

	if i != 1 {
		panic("fail to callback", i)
	}
}