GithubHelp home page GithubHelp logo

singchia / go-timer Goto Github PK

View Code? Open in Web Editor NEW
81.0 2.0 6.0 7.69 MB

⏰ A fast go timer with minimal goroutines.

License: Apache License 2.0

Go 100.00%
timingwheel high-performance algorithms-datastructures event-driven tick timer

go-timer's Introduction

GO-TIMER

Go License Go Report Card

Overview

A high performance timer with minimal goroutines.

How it works

Features

  • One goroutine runs all
  • Goroutine safe
  • Clean and simple, no third-party deps at all
  • High performance with timing-wheels algorithm
  • Minimal resources use
  • Managed data and handler
  • Customizing channel
  • Well tested

Usage

Quick Start

package main

import (
	"log"
	"time"

	timer "github.com/singchia/go-timer/v2"
)

func main() {
	t1 := time.Now()
	// new timer
	t := timer.NewTimer()
	// add a tick in 1s
	tick := t.Add(time.Second)
	// wait for it
	<-tick.C()
	// tick fired as time is up, calculate and print the elapse
	log.Printf("time elapsed: %fs\n", time.Now().Sub(t1).Seconds())
}

Async handler

package main

import (
	"log"
	"sync"
	"time"

	timer "github.com/singchia/go-timer/v2"
)

func main() {
	// we need a wait group since using async handler
	wg := sync.WaitGroup{}
	wg.Add(1)
	// new timer
	t := timer.NewTimer()
	// add a tick in 1s with current time and a async handler
	t.Add(time.Second, timer.WithData(time.Now()), timer.WithHandler(func(event *timer.Event) {
		defer wg.Done()
		// tick fired as time is up, calculate and print the elapse
		log.Printf("time elapsed: %fs\n", time.Now().Sub(event.Data.(time.Time)).Seconds())
	}))

	wg.Wait()
}

With cyclically

package main

import (
	"log"
	"time"

	timer "github.com/singchia/go-timer/v2"
)

func main() {
	t1 := time.Now()
	// new timer
	t := timer.NewTimer()
	// add cyclical tick in 1s
	tick := t.Add(time.Second, timer.WithCyclically())
	for {
		// wait for it cyclically
		<-tick.C()
		t2 := time.Now()
		// calculate and print the elapse
		log.Printf("time elapsed: %fs\n", t2.Sub(t1).Seconds())
		t1 = t2
	}
}

License

© Austin Zhai, 2015-2025

Released under the Apache License 2.0

go-timer's People

Contributors

singchia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

go-timer's Issues

The calculation on cyclically tick is incorrect.

The code:

func (tw *timingwheel) indexesPerWheelBased(d time.Duration, base []uint) []uint {
	var ipw []uint
	var reminder uint64
	var quotient = uint64((d + tw.interval - 1) / tw.interval)
	for i, wheel := range tw.wheels {
		if quotient == 0 {
			break
		}
		if len(base) <= i {
			ipw = append(ipw, uint(quotient))
			break
		}
		quotient += uint64(base[i])
		reminder = quotient % uint64(wheel.numSlots)
		quotient = quotient / uint64(wheel.numSlots)
		ipw = append(ipw, uint(reminder))
	}
	return ipw
}

ipw = append(ipw, uint(quotient)) missed the overflow case while the quotient bigger then interval.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.