2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
2023-01-18 09:56:42 -07:00
|
|
|
"sync"
|
2019-11-19 10:00:20 -07:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// How many timer objects should be cached
|
|
|
|
const timerCacheMax = 50000
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
type TimerWheel[T any] struct {
|
2019-11-19 10:00:20 -07:00
|
|
|
// Current tick
|
|
|
|
current int
|
|
|
|
|
|
|
|
// Cheat on finding the length of the wheel
|
|
|
|
wheelLen int
|
|
|
|
|
|
|
|
// Last time we ticked, since we are lazy ticking
|
|
|
|
lastTick *time.Time
|
|
|
|
|
|
|
|
// Durations of a tick and the entire wheel
|
|
|
|
tickDuration time.Duration
|
|
|
|
wheelDuration time.Duration
|
|
|
|
|
|
|
|
// The actual wheel which is just a set of singly linked lists, head/tail pointers
|
2023-01-18 09:56:42 -07:00
|
|
|
wheel []*TimeoutList[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Singly linked list of items that have timed out of the wheel
|
2023-01-18 09:56:42 -07:00
|
|
|
expired *TimeoutList[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Item cache to avoid garbage collect
|
2023-01-18 09:56:42 -07:00
|
|
|
itemCache *TimeoutItem[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
itemsCached int
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
type LockingTimerWheel[T any] struct {
|
|
|
|
m sync.Mutex
|
|
|
|
t *TimerWheel[T]
|
|
|
|
}
|
|
|
|
|
2023-01-11 18:35:19 -07:00
|
|
|
// TimeoutList Represents a tick in the wheel
|
2023-01-18 09:56:42 -07:00
|
|
|
type TimeoutList[T any] struct {
|
|
|
|
Head *TimeoutItem[T]
|
|
|
|
Tail *TimeoutItem[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-01-11 18:35:19 -07:00
|
|
|
// TimeoutItem Represents an item within a tick
|
2023-01-18 09:56:42 -07:00
|
|
|
type TimeoutItem[T any] struct {
|
|
|
|
Item T
|
|
|
|
Next *TimeoutItem[T]
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-01-11 18:35:19 -07:00
|
|
|
// NewTimerWheel Builds a timer wheel and identifies the tick duration and wheel duration from the provided values
|
2019-11-19 10:00:20 -07:00
|
|
|
// Purge must be called once per entry to actually remove anything
|
2023-01-18 09:56:42 -07:00
|
|
|
// The TimerWheel does not handle concurrency on its own.
|
|
|
|
// Locks around access to it must be used if multiple routines are manipulating it.
|
|
|
|
func NewTimerWheel[T any](min, max time.Duration) *TimerWheel[T] {
|
2019-11-19 10:00:20 -07:00
|
|
|
//TODO provide an error
|
|
|
|
//if min >= max {
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
|
2023-01-11 18:35:19 -07:00
|
|
|
// Round down and add 2 so we can have the smallest # of ticks in the wheel and still account for a full
|
|
|
|
// max duration, even if our current tick is at the maximum position and the next item to be added is at maximum
|
|
|
|
// timeout
|
|
|
|
wLen := int((max / min) + 2)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
tw := TimerWheel[T]{
|
2019-11-19 10:00:20 -07:00
|
|
|
wheelLen: wLen,
|
2023-01-18 09:56:42 -07:00
|
|
|
wheel: make([]*TimeoutList[T], wLen),
|
2019-11-19 10:00:20 -07:00
|
|
|
tickDuration: min,
|
|
|
|
wheelDuration: max,
|
2023-01-18 09:56:42 -07:00
|
|
|
expired: &TimeoutList[T]{},
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := range tw.wheel {
|
2023-01-18 09:56:42 -07:00
|
|
|
tw.wheel[i] = &TimeoutList[T]{}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return &tw
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
// NewLockingTimerWheel is version of TimerWheel that is safe for concurrent use with a small performance penalty
|
|
|
|
func NewLockingTimerWheel[T any](min, max time.Duration) *LockingTimerWheel[T] {
|
|
|
|
return &LockingTimerWheel[T]{
|
|
|
|
t: NewTimerWheel[T](min, max),
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
// Add will add an item to the wheel in its proper timeout.
|
|
|
|
// Caller should Advance the wheel prior to ensure the proper slot is used.
|
|
|
|
func (tw *TimerWheel[T]) Add(v T, timeout time.Duration) *TimeoutItem[T] {
|
2019-11-19 10:00:20 -07:00
|
|
|
i := tw.findWheel(timeout)
|
|
|
|
|
|
|
|
// Try to fetch off the cache
|
|
|
|
ti := tw.itemCache
|
|
|
|
if ti != nil {
|
|
|
|
tw.itemCache = ti.Next
|
|
|
|
tw.itemsCached--
|
|
|
|
ti.Next = nil
|
|
|
|
} else {
|
2023-01-18 09:56:42 -07:00
|
|
|
ti = &TimeoutItem[T]{}
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Relink and return
|
2023-01-18 09:56:42 -07:00
|
|
|
ti.Item = v
|
2019-11-19 10:00:20 -07:00
|
|
|
if tw.wheel[i].Tail == nil {
|
|
|
|
tw.wheel[i].Head = ti
|
|
|
|
tw.wheel[i].Tail = ti
|
|
|
|
} else {
|
|
|
|
tw.wheel[i].Tail.Next = ti
|
|
|
|
tw.wheel[i].Tail = ti
|
|
|
|
}
|
|
|
|
|
|
|
|
return ti
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
// Purge removes and returns the first available expired item from the wheel and the 2nd argument is true.
|
|
|
|
// If no item is available then an empty T is returned and the 2nd argument is false.
|
|
|
|
func (tw *TimerWheel[T]) Purge() (T, bool) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if tw.expired.Head == nil {
|
2023-01-18 09:56:42 -07:00
|
|
|
var na T
|
|
|
|
return na, false
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ti := tw.expired.Head
|
|
|
|
tw.expired.Head = ti.Next
|
|
|
|
|
|
|
|
if tw.expired.Head == nil {
|
|
|
|
tw.expired.Tail = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear out the items references
|
|
|
|
ti.Next = nil
|
|
|
|
|
|
|
|
// Maybe cache it for later
|
|
|
|
if tw.itemsCached < timerCacheMax {
|
|
|
|
ti.Next = tw.itemCache
|
|
|
|
tw.itemCache = ti
|
|
|
|
tw.itemsCached++
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
return ti.Item, true
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
// findWheel find the next position in the wheel for the provided timeout given the current tick
|
|
|
|
func (tw *TimerWheel[T]) findWheel(timeout time.Duration) (i int) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if timeout < tw.tickDuration {
|
|
|
|
// Can't track anything below the set resolution
|
|
|
|
timeout = tw.tickDuration
|
|
|
|
} else if timeout > tw.wheelDuration {
|
|
|
|
// We aren't handling timeouts greater than the wheels duration
|
|
|
|
timeout = tw.wheelDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the next highest, rounding up
|
|
|
|
tick := int(((timeout - 1) / tw.tickDuration) + 1)
|
|
|
|
|
|
|
|
// Add another tick since the current tick may almost be over then map it to the wheel from our
|
|
|
|
// current position
|
|
|
|
tick += tw.current + 1
|
|
|
|
if tick >= tw.wheelLen {
|
|
|
|
tick -= tw.wheelLen
|
|
|
|
}
|
|
|
|
|
|
|
|
return tick
|
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
// Advance will move the wheel forward by the appropriate number of ticks for the provided time and all items
|
|
|
|
// passed over will be moved to the expired list. Calling Purge is necessary to remove them entirely.
|
|
|
|
func (tw *TimerWheel[T]) Advance(now time.Time) {
|
2019-11-19 10:00:20 -07:00
|
|
|
if tw.lastTick == nil {
|
|
|
|
tw.lastTick = &now
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to round down
|
|
|
|
ticks := int(now.Sub(*tw.lastTick) / tw.tickDuration)
|
|
|
|
adv := ticks
|
|
|
|
if ticks > tw.wheelLen {
|
|
|
|
ticks = tw.wheelLen
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < ticks; i++ {
|
|
|
|
tw.current++
|
|
|
|
if tw.current >= tw.wheelLen {
|
|
|
|
tw.current = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if tw.wheel[tw.current].Head != nil {
|
|
|
|
// We need to append the expired items as to not starve evicting the oldest ones
|
|
|
|
if tw.expired.Tail == nil {
|
|
|
|
tw.expired.Head = tw.wheel[tw.current].Head
|
|
|
|
tw.expired.Tail = tw.wheel[tw.current].Tail
|
|
|
|
} else {
|
|
|
|
tw.expired.Tail.Next = tw.wheel[tw.current].Head
|
|
|
|
tw.expired.Tail = tw.wheel[tw.current].Tail
|
|
|
|
}
|
|
|
|
|
|
|
|
tw.wheel[tw.current].Head = nil
|
|
|
|
tw.wheel[tw.current].Tail = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance the tick based on duration to avoid losing some accuracy
|
|
|
|
newTick := tw.lastTick.Add(tw.tickDuration * time.Duration(adv))
|
|
|
|
tw.lastTick = &newTick
|
|
|
|
}
|
2023-01-18 09:56:42 -07:00
|
|
|
|
|
|
|
func (lw *LockingTimerWheel[T]) Add(v T, timeout time.Duration) *TimeoutItem[T] {
|
|
|
|
lw.m.Lock()
|
|
|
|
defer lw.m.Unlock()
|
|
|
|
return lw.t.Add(v, timeout)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *LockingTimerWheel[T]) Purge() (T, bool) {
|
|
|
|
lw.m.Lock()
|
|
|
|
defer lw.m.Unlock()
|
|
|
|
return lw.t.Purge()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lw *LockingTimerWheel[T]) Advance(now time.Time) {
|
|
|
|
lw.m.Lock()
|
|
|
|
defer lw.m.Unlock()
|
|
|
|
lw.t.Advance(now)
|
|
|
|
}
|