timer

package
v0.0.0-...-89d789c Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package timer provides various enhanced timer functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RandTicker

type RandTicker struct {
	C <-chan time.Time
	// contains filtered or unexported fields
}

RandTicker is just like time.Ticker, except that it adds randomness to the events.

func NewRandTicker

func NewRandTicker(d, variance time.Duration) *RandTicker

NewRandTicker creates a new RandTicker. d is the duration, and variance specifies the variance. The ticker will tick every d +/- variance.

func (*RandTicker) Stop

func (tkr *RandTicker) Stop()

Stop stops the ticker and closes the underlying channel.

type Task

type Task struct {
	// contains filtered or unexported fields
}

Task means handle unit in time wheel

type TimeWheel

type TimeWheel struct {
	// contains filtered or unexported fields
}

TimeWheel means time wheel

func NewTimeWheel

func NewTimeWheel(tick time.Duration, bucketsNum int) (*TimeWheel, error)

NewTimeWheel create new time wheel

func (*TimeWheel) Add

func (tw *TimeWheel) Add(delay time.Duration, key interface{}, callback func()) error

Add add an item into time wheel

func (*TimeWheel) Remove

func (tw *TimeWheel) Remove(key interface{}) error

Remove remove an item from time wheel

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start start the time wheel

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop stop the time wheel

type Timer

type Timer struct {
	// contains filtered or unexported fields
}

Timer provides timer functionality that can be controlled by the user. You start the timer by providing it a callback function, which it will call at the specified interval.

var t = timer.NewTimer(1e9)
t.Start(KeepHouse)

func KeepHouse() {
	// do house keeping work
}

You can stop the timer by calling t.Stop, which is guaranteed to wait if KeepHouse is being executed.

You can create an untimely trigger by calling t.Trigger. You can also schedule an untimely trigger by calling t.TriggerAfter.

The timer interval can be changed on the fly by calling t.SetInterval. A zero value interval will cause the timer to wait indefinitely, and it will react only to an explicit Trigger or Stop.

func NewTimer

func NewTimer(interval time.Duration) *Timer

NewTimer creates a new Timer object

func (*Timer) Interval

func (tm *Timer) Interval() time.Duration

Interval returns the current interval.

func (*Timer) Running

func (tm *Timer) Running() bool

func (*Timer) SetInterval

func (tm *Timer) SetInterval(ns time.Duration)

SetInterval changes the wait interval. It will cause the timer to restart the wait.

func (*Timer) Start

func (tm *Timer) Start(keephouse func())

Start starts the timer.

func (*Timer) Stop

func (tm *Timer) Stop()

Stop will stop the timer. It guarantees that the timer will not execute any more calls to keephouse once it has returned.

func (*Timer) Trigger

func (tm *Timer) Trigger()

Trigger will cause the timer to immediately execute the keephouse function. It will then cause the timer to restart the wait.

func (*Timer) TriggerAfter

func (tm *Timer) TriggerAfter(duration time.Duration)

TriggerAfter waits for the specified duration and triggers the next event.

Jump to

Keyboard shortcuts

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