Skip to content
/ timer Public

Go implementation of Kafka's Hierarchical Timing Wheels. Support millions of tasks. 高性能层级时间轮定时器(理论无限分级轮)

License

Notifications You must be signed in to change notification settings

thinkgos/timer

Repository files navigation

timer

Go implementation of Kafka's Hierarchical Timing Wheels.

Go.Dev reference codecov Tests Go Report Card Licence Tag

Feature

  • Unlimited hierarchical wheel.
  • insert, delete, scan task almost O(1).
  • Different from the time wheel of Linux, it has no maximum time limit.
  • Support millions of tasks.
  • timed is a global timer instance, that tick is 1ms. wheel size is 512, use ants goroutine pool.

Usage

Installation

Use go get.

    go get github.com/thinkgos/timer

Then import the package into your own code.

    import "github.com/thinkgos/timer"

Example

use global timer instance.

package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/thinkgos/timer/timed"
)

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		index := i
		_, _ = timed.AfterFunc(time.Duration(i)*100*time.Millisecond, func() {
			fmt.Printf("%s: timer task %d is executed, remain task: %d\n", time.Now().String(), index, timed.TaskCounter())
			wg.Done()
		})
	}
	wg.Wait()
}

References

License

This project is under MIT License. See the LICENSE file for the full license text.

About

Go implementation of Kafka's Hierarchical Timing Wheels. Support millions of tasks. 高性能层级时间轮定时器(理论无限分级轮)

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages