asynctimer

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2024 License: Apache-2.0, BSD-2-Clause, BSD-3-Clause, + 1 more Imports: 4 Imported by: 0

Documentation

Overview

Package asynctimer provides asynchronous timer function, which is implemented by time wheel.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidParam denotes that param is invalid.
	ErrInvalidParam = errors.New("asynctimer: param is invalid")
	// ErrShortDelay denotes that timer's delay is too short.
	ErrShortDelay = errors.New("asynctimer: delay time is too short")
)

Functions

func Add

func Add(timer *Timer) error

Add adds timer to the default time wheel. Timer becomes effective after having been added. Note that time precision is 1 second, if the required precision is less than 1s, a custom time wheel should be used.

func Del

func Del(timer *Timer)

Del deletes timer from the default time wheel. After having been deleted, timer becomes invalid, and the resource will be released.

func Stop

func Stop()

Stop stops the default time wheel.

Types

type Callback

type Callback func(data any)

Callback is asynchronous callback function when timer expired.

type TimeWheel

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

TimeWheel manages all async timers.

func NewTimeWheel

func NewTimeWheel(interval time.Duration, slotNum int) (*TimeWheel, error)

NewTimeWheel creates a time wheel which manages async timers. Time wheel's round time is equal to slotNum*interval. Interval means duration of each tick of time wheel, slotNum means numbers of slots in a round.

func (*TimeWheel) Add

func (t *TimeWheel) Add(timer *Timer) error

Add adds timer to time wheel. Timer becomes effective after having been added to time wheel.

func (*TimeWheel) Del

func (t *TimeWheel) Del(timer *Timer)

Del deletes timer from time wheel. After having been deleted from the wheel, timer become invalid, and the resource will be released.

func (*TimeWheel) Start

func (t *TimeWheel) Start()

Start starts the time wheel. Make sure to start the time wheel before using it.

func (*TimeWheel) Stop

func (t *TimeWheel) Stop()

Stop stops the time wheel, all related resource managed by the time wheel will be released.

type Timer

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

Timer is an async timer.

func NewTimer

func NewTimer(data any, expiredHandle Callback, timeout time.Duration) *Timer

NewTimer creates an async timer with data and expiredHandle function. After timeout time, the timer will be expired, and expiredHandle will be called with argument data. Note that the timer becomes effective after having been added to time wheel.

Jump to

Keyboard shortcuts

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