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 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
}
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 task total number of tasks.
type TimingWheel ¶
type TimingWheel struct {
// contains filtered or unexported fields
}
Click to show internal directories.
Click to hide internal directories.