cherryTimeWheel

package
v1.3.12 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package cherryTimeWheel file from https://github.com/RussellLuo/timingwheel

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MSToTime

func MSToTime(t int64) time.Time

MSToTime returns the UTC time corresponding to the given Unix time, t milliseconds since January 1, 1970 UTC.

func NextId

func NextId() uint64

func TimeToMS

func TimeToMS(t time.Time) int64

TimeToMS returns an integer number, which represents t in milliseconds.

Types

type DelayQueue

type DelayQueue struct {
	C chan interface{}
	// contains filtered or unexported fields
}

DelayQueue is an unbounded blocking queue of *Delayed* elements, in which an element can only be taken when its delay has expired. The head of the queue is the *Delayed* element whose delay expired furthest in the past.

func NewDelayQueue

func NewDelayQueue(size int) *DelayQueue

NewDelayQueue creates an instance of delayQueue with the specified size.

func (*DelayQueue) Offer

func (dq *DelayQueue) Offer(elem interface{}, expiration int64)

Offer inserts the element into the current queue.

func (*DelayQueue) Poll

func (dq *DelayQueue) Poll(exitC chan struct{}, nowF func() int64)

Poll starts an infinite loop, in which it continually waits for an element to expire and then send the expired element to the channel C.

type EverySchedule

type EverySchedule struct {
	Interval time.Duration
}

func (*EverySchedule) Next

func (s *EverySchedule) Next(prev time.Time) time.Time

type FixedDateSchedule

type FixedDateSchedule struct {
	Hour, Minute, Second int
}

func (*FixedDateSchedule) Next

func (s *FixedDateSchedule) Next(prev time.Time) time.Time

type Scheduler

type Scheduler interface {
	// Next returns the next execution time after the given (previous) time.
	// It will return a zero time if no next time is scheduled.
	//
	// All times must be UTC.
	Next(time.Time) time.Time
}

Scheduler determines the execution plan of a task.

type TimeWheel

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

TimeWheel is an implementation of Hierarchical Timing Wheels.

func NewTimeWheel

func NewTimeWheel(tick time.Duration, wheelSize int64) *TimeWheel

NewTimeWheel creates an instance of TimeWheel with the given tick and wheelSize.

func (*TimeWheel) AddEveryFunc

func (tw *TimeWheel) AddEveryFunc(id uint64, d time.Duration, f func(), async ...bool) *Timer

func (*TimeWheel) AfterFunc

func (tw *TimeWheel) AfterFunc(id uint64, d time.Duration, f func(), async ...bool) *Timer

AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.

func (*TimeWheel) BuildAfterFunc

func (tw *TimeWheel) BuildAfterFunc(d time.Duration, f func()) *Timer

func (*TimeWheel) BuildEveryFunc

func (tw *TimeWheel) BuildEveryFunc(d time.Duration, f func(), async ...bool) *Timer

func (*TimeWheel) NextId

func (tw *TimeWheel) NextId() uint64

func (*TimeWheel) ScheduleFunc

func (tw *TimeWheel) ScheduleFunc(id uint64, s Scheduler, f func(), async ...bool) *Timer

ScheduleFunc calls f (in its own goroutine) according to the execution plan scheduled by s. It returns a Timer that can be used to cancel the call using its Stop method.

If the caller want to terminate the execution plan halfway, it must stop the timer and ensure that the timer is stopped actually, since in the current implementation, there is a gap between the expiring and the restarting of the timer. The wait time for ensuring is short since the gap is very small.

Internally, ScheduleFunc will ask the first execution time (by calling s.Next()) initially, and create a timer if the execution time is non-zero. Afterwards, it will ask the next execution time each time f is about to be executed, and f will be called at the next execution time if the time is non-zero.

func (*TimeWheel) Start

func (tw *TimeWheel) Start()

Start starts the current timing wheel.

func (*TimeWheel) Stop

func (tw *TimeWheel) Stop()

Stop stops the current timing wheel.

If there is any timer's task being running in its own goroutine, Stop does not wait for the task to complete before returning. If the caller needs to know whether the task is completed, it must coordinate with the task explicitly.

type Timer

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

Timer represents a single event. When the Timer expires, the given task will be executed.

func (*Timer) ID

func (t *Timer) ID() uint64

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped.

If the timer t has already expired and the t.task has been started in its own goroutine; Stop does not wait for t.task to complete before returning. If the caller needs to know whether t.task is completed, it must coordinate with t.task explicitly.

Jump to

Keyboard shortcuts

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