timer

package module
v0.1.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2024 License: MIT Imports: 8 Imported by: 0

README

timer

Go implementation of Kafka's Hierarchical Timing Wheels.

Go.Dev reference codecov Tests Go Report Card Licence Tag

  • timer Go implementation of Kafka's Hierarchical Timing Wheels.
  • timed global timer instance, that tick is 1ms. wheel size is 1024, use ants goroutine pool.

Usage

Installation

Use go get.

    go get github.com/thinkgos/timer

Then import the package into your own code.

    import "github.com/thinkgos/timer"
Example
package main

import (
	"fmt"
	"sync"
	"time"

	"github.com/thinkgos/timer/timed"
)

func main() {
	var wg sync.WaitGroup
	for i := 0; i < 1000; i++ {
		wg.Add(1)
		index := i
		_, _ = timed.AfterFunc(time.Duration(i)*100*time.Millisecond, func() {
			fmt.Printf("%s: timer task %d is executed, remain task: %d\n", time.Now().String(), index, timed.TaskCounter())
			wg.Done()
		})
	}
	wg.Wait()
}

References

License

This project is under MIT License. See the LICENSE file for the full license text.

Documentation

Index

Examples

Constants

View Source
const (
	DefaultTickMs    int64 = 1
	DefaultWheelSize       = 1024
)

Variables

View Source
var (
	ErrClosed = errors.New("timer: use of closed timer")
)

Functions

func IsPowOf2

func IsPowOf2(x int) bool

func NextPowOf2

func NextPowOf2(x int) int

Types

type GoPool

type GoPool interface {
	Go(f func())
}

type Job

type Job interface {
	Run()
}

Job job interface

type JobFunc

type JobFunc func()

JobFunc job function

func (JobFunc) Run

func (f JobFunc) Run()

Run implement job interface

type Option

type Option func(*Timer)

func WithGoPool

func WithGoPool(p GoPool) Option

WithGoPool 设置协程池

func WithTickMs

func WithTickMs(tickMs int64) Option

WithTickMs 设置基本时间跨度

func WithWheelSize

func WithWheelSize(size int) Option

WithWheelSize 设置时间轮大小

type Spoke

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

func NewSpoke

func NewSpoke(taskCounter *atomic.Int64) *Spoke

func (*Spoke) Add

func (sp *Spoke) Add(task *Task)

Add a timer task to this list

func (*Spoke) CompareTo

func (sp *Spoke) CompareTo(sp2 queue.Comparable) int

func (*Spoke) DelayMs

func (sp *Spoke) DelayMs() int64

func (*Spoke) Flush

func (sp *Spoke) Flush(f func(*Task))

Flush all task entries and apply the supplied function to each of them

func (*Spoke) GetExpiration

func (sp *Spoke) GetExpiration() int64

Get the spoke's expiration time

func (*Spoke) Remove

func (sp *Spoke) Remove(task *Task)

Remove the specified timer task from this list

func (*Spoke) SetExpiration

func (sp *Spoke) SetExpiration(expirationMs int64) bool

Set the spoke's expiration time Returns true if the expiration time is changed

type Task

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

Task 是双向链表的一个元素.

func NewTask

func NewTask(d time.Duration) *Task

NewTask new task with delay duration and empty job, the accuracy is milliseconds.

func NewTaskFunc

func NewTaskFunc(d time.Duration, f func()) *Task

NewTaskFunc new task with delay duration and function job, the accuracy is milliseconds.

func (*Task) Cancel

func (t *Task) Cancel()

Cancel the task

func (*Task) Delay

func (t *Task) Delay() time.Duration

Delay delay duration, the accuracy is milliseconds.

func (*Task) ExpirationMs

func (t *Task) ExpirationMs() int64

ExpirationMs expiration milliseconds.

func (*Task) Run

func (t *Task) Run()

Run immediate call job.

func (*Task) WithJob

func (t *Task) WithJob(j Job) *Task

WithJob with job

func (*Task) WithJobFunc

func (t *Task) WithJobFunc(f func()) *Task

WithJobFunc with function job

type Timer

type Timer struct {
	// contains filtered or unexported fields
}
Example
tm := NewTimer()
tm.Start()
_, _ = tm.AfterFunc(100*time.Millisecond, func() {
	fmt.Println(100)
})
_ = tm.AddTask(NewTask(1025 * time.Millisecond).WithJobFunc(func() {
	fmt.Println(200)
}))
canceledTaskAfterAdd := NewTask(300 * time.Millisecond).WithJobFunc(func() {
	fmt.Println("canceled after add")
})
_ = tm.AddTask(canceledTaskAfterAdd)
canceledTaskAfterAdd.Cancel()
canceledTaskBeforeAdd := NewTask(301 * time.Millisecond).WithJobFunc(func() {
	fmt.Println("canceled before add")
})
canceledTaskBeforeAdd.Cancel()
_ = tm.AddTask(canceledTaskBeforeAdd)
time.Sleep(time.Second + time.Millisecond*200)
tm.Stop()
Output:

100
200

func NewTimer

func NewTimer(opts ...Option) *Timer

NewTimer new timer instance. tick is 1 milliseconds, wheel size is 1024.

func (*Timer) AddTask

func (t *Timer) AddTask(task *Task) error

AddTask adds a task to the timer.

func (*Timer) AfterFunc

func (t *Timer) AfterFunc(d time.Duration, f func()) (*Task, error)

AfterFunc adds a function to the timer.

func (*Timer) Start

func (t *Timer) Start()

Start the timer.

func (*Timer) Started

func (t *Timer) Started() bool

Started have started or not.

func (*Timer) Stop

func (t *Timer) Stop()

Stop the timer.

func (*Timer) TaskCounter

func (t *Timer) TaskCounter() int64

TaskCounter task total number of tasks.

func (*Timer) TickMs

func (t *Timer) TickMs() int64

TickMs Basic time tick milliseconds.

func (*Timer) WheelSize

func (t *Timer) WheelSize() int

WheelSize wheel size.

type TimingWheel

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

Directories

Path Synopsis
_examples
sample command

Jump to

Keyboard shortcuts

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