Documentation
¶
Index ¶
- Constants
- Variables
- func IsPowOf2(x int) bool
- func NextPowOf2(x int) int
- type GoPool
- type Job
- type JobFunc
- type Option
- type Spoke
- type Task
- type Timer
- func (t *Timer) AddTask(task *Task) error
- func (t *Timer) AfterFunc(d time.Duration, f func()) (*Task, error)
- func (t *Timer) Start()
- func (t *Timer) Started() bool
- func (t *Timer) Stop()
- func (t *Timer) TaskCounter() int64
- func (t *Timer) TickMs() int64
- func (t *Timer) WheelMask() int
- func (t *Timer) WheelSize() int
- type TimingWheel
Examples ¶
Constants ¶
View Source
const ( // DefaultTickMs default tick milliseconds. DefaultTickMs = 1 // DefaultWheelSize default wheel size. DefaultWheelSize = 1024 )
Variables ¶
View Source
var ( // ErrClosed is returned when the timer is closed. ErrClosed = errors.New("timer: use of closed timer") )
Functions ¶
func NextPowOf2 ¶
Types ¶
type Spoke ¶
type Spoke struct {
// contains filtered or unexported fields
}
func (*Spoke) GetExpiration ¶
Get the spoke's expiration time
func (*Spoke) SetExpiration ¶
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 NewTaskFunc ¶
NewTaskFunc new task with delay duration and function job, the accuracy is milliseconds.
func (*Task) ExpirationMs ¶
ExpirationMs expiration milliseconds.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
Timer is a timer
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 (*Timer) TaskCounter ¶
TaskCounter the total number of tasks.
type TimingWheel ¶
type TimingWheel struct {
// contains filtered or unexported fields
}
Click to show internal directories.
Click to hide internal directories.