timewheel

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

go-timewheel

golang timewheel lib, similar to golang std timer

source

Usage

base method

init timewheel

tw, err := NewTimeWheel(1 * time.Second, 360)
if err != nil {
    panic(err)
}

tw.Start()
tw.Stop()

safe ticker

tw, _ := NewTimeWheel(1 * time.Second, 360, TickSafeMode())

use sync.Pool

tw, _ := NewTimeWheel(1 * time.Second, 360, SetSyncPool(true))

add delay task

task := tw.Add(5 * time.Second, func(){})

remove delay task

tw.Remove(task)

add cron delay task

task := tw.AddCron(5 * time.Second, func(){
    ...
})
similar to std time

similar to time.Sleep

tw.Sleep(5 * time.Second)

similar to time.After()

<- tw.After(5 * time.Second)

similar to time.NewTimer

timer :=tw.NewTimer(5 * time.Second)
<- timer.C
timer.Reset(1 * time.Second)
timer.Stop()

similar to time.NewTicker

timer :=tw.NewTicker(5 * time.Second)
<- timer.C
timer.Stop()

similar to time.AfterFunc

runner :=tw.AfterFunc(5 * time.Second, func(){})
<- runner.C
runner.Stop()

benchmark test

example/main.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultTimeWheel, _ = NewTimeWheel(time.Second, 120)
)

Functions

func After

func After(delay time.Duration) <-chan time.Time

func Remove

func Remove(task *Task) error

func ResetDefaultTimeWheel

func ResetDefaultTimeWheel(tw *TimeWheel)

func SetSyncPool

func SetSyncPool(state bool) optionCall

func Sleep

func Sleep(delay time.Duration)

func TickSafeMode

func TickSafeMode() optionCall

Types

type Task

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

func Add

func Add(delay time.Duration, callback func()) *Task

func AddCron

func AddCron(delay time.Duration, callback func()) *Task

func (*Task) Reset

func (t *Task) Reset()

for sync.Pool

type Ticker

type Ticker struct {
	C   chan bool
	Ctx context.Context
	// contains filtered or unexported fields
}

func NewTicker

func NewTicker(delay time.Duration) *Ticker

func (*Ticker) Stop

func (t *Ticker) Stop()

type TimeWheel

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

func NewTimeWheel

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

NewTimeWheel create new time wheel

func (*TimeWheel) Add

func (tw *TimeWheel) Add(delay time.Duration, callback func()) *Task

Add add an task

func (*TimeWheel) AddCron

func (tw *TimeWheel) AddCron(delay time.Duration, callback func()) *Task

AddCron add interval task

func (*TimeWheel) After

func (tw *TimeWheel) After(delay time.Duration) <-chan time.Time

func (*TimeWheel) AfterFunc

func (tw *TimeWheel) AfterFunc(delay time.Duration, callback func()) *Timer

func (*TimeWheel) NewTicker

func (tw *TimeWheel) NewTicker(delay time.Duration) *Ticker

func (*TimeWheel) NewTimer

func (tw *TimeWheel) NewTimer(delay time.Duration) *Timer

func (*TimeWheel) Remove

func (tw *TimeWheel) Remove(task *Task) error

func (*TimeWheel) Sleep

func (tw *TimeWheel) Sleep(delay time.Duration)

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 TimeWheelPool

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

func NewTimeWheelPool

func NewTimeWheelPool(size int, tick time.Duration, bucketsNum int, options ...optionCall) (*TimeWheelPool, error)

func (*TimeWheelPool) Get

func (tp *TimeWheelPool) Get() *TimeWheel

func (*TimeWheelPool) GetRandom

func (tp *TimeWheelPool) GetRandom() *TimeWheel

func (*TimeWheelPool) Start

func (tp *TimeWheelPool) Start()

func (*TimeWheelPool) Stop

func (tp *TimeWheelPool) Stop()

type Timer

type Timer struct {
	C chan bool

	Ctx context.Context
	// contains filtered or unexported fields
}

similar to golang std timer

func AfterFunc

func AfterFunc(delay time.Duration, callback func()) *Timer

func NewTimer

func NewTimer(delay time.Duration) *Timer

func (*Timer) AddStopFunc

func (t *Timer) AddStopFunc(callback func())

func (*Timer) Reset

func (t *Timer) Reset(delay time.Duration)

func (*Timer) Stop

func (t *Timer) Stop()

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

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