timer

package module
v2.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2023 License: Apache-2.0 Imports: 7 Imported by: 13

README

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

Documentation

Overview

* Copyright (c) 2021 Austin Zhai <singchia@163.com> * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrDurationOutOfRange   = errors.New("duration out of range")
	ErrTimerNotStarted      = errors.New("timer not started")
	ErrTimerForceClosed     = errors.New("timer force closed")
	ErrOperationForceClosed = errors.New("operation force closed")
	ErrDelayOnCyclically    = errors.New("cannot delay on cyclically tick")
	ErrCancelOnNonWait      = errors.New("cannot cancel on firing or fired tick")
)

Functions

This section is empty.

Types

type Event

type Event struct {
	Duration   time.Duration
	InsertTIme time.Time
	Data       interface{}
	Error      error
}

type Tick

type Tick interface {
	//To reset the data set at Timer.Time()
	Reset(data interface{}) error

	//To cancel the tick
	Cancel() error

	//Delay the tick
	Delay(d time.Duration) error

	//To get the channel called at Timer.Time(),
	//you will get the same channel if set, if not and handler is nil,
	//then a new created channel will be returned.
	C() <-chan *Event

	// Insert time
	InsertTime() time.Time

	// The tick duration original set
	Duration() time.Duration

	// Fired count
	Fired() int64
}

Tick that set in Timer can be required from Timer.Add()

type TickOption

type TickOption func(*tickOption)

func WithChan

func WithChan(C chan *Event) TickOption

func WithCyclically

func WithCyclically() TickOption

func WithData

func WithData(data interface{}) TickOption

func WithHandler

func WithHandler(handler func(*Event)) TickOption

type Timer

type Timer interface {
	Add(d time.Duration, opts ...TickOption) Tick

	// Close to close timer,
	// all ticks set would be discarded.
	Close()

	// Pause the timer,
	// all ticks won't continue after Timer.Movenon().
	Pause()

	// Continue the paused timer.
	Moveon()
}

func NewTimer

func NewTimer(opts ...TimerOption) Timer

Entry

type TimerOption

type TimerOption func(*timerOption)

func WithOperationBufferSize added in v2.2.0

func WithOperationBufferSize(n int) TimerOption

func WithTimeInterval

func WithTimeInterval(interval time.Duration) TimerOption

Directories

Path Synopsis
example
pkg
test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL