Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ func (b *bucket) Flush(reinsert func(*Timer)) {

e = next
}
b.SetExpiration(-1)
b.mu.Unlock()

b.SetExpiration(-1) // TODO: Improve the coordination with b.Add()

for _, t := range ts {
reinsert(t)
}
Expand Down
11 changes: 10 additions & 1 deletion timingwheel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package timingwheel

import (
"errors"
"sync"
"sync/atomic"
"time"
"unsafe"

"github.com/RussellLuo/timingwheel/delayqueue"
)

var rwlock *sync.RWMutex

// TimingWheel is an implementation of Hierarchical Timing Wheels.
type TimingWheel struct {
tick int64 // in milliseconds
Expand Down Expand Up @@ -109,7 +112,10 @@ func (tw *TimingWheel) add(t *Timer) bool {
// addOrRun inserts the timer t into the current timing wheel, or run the
// timer's task if it has already expired.
func (tw *TimingWheel) addOrRun(t *Timer) {
if !tw.add(t) {
rwlock.RLock()
notExpired := tw.add(t)
rwlock.RUnlock()
if !notExpired {
// Already expired

// Like the standard time.AfterFunc (https://golang.org/pkg/time/#AfterFunc),
Expand All @@ -134,6 +140,7 @@ func (tw *TimingWheel) advanceClock(expiration int64) {

// Start starts the current timing wheel.
func (tw *TimingWheel) Start() {
rwlock = new(sync.RWMutex)
tw.waitGroup.Wrap(func() {
tw.queue.Poll(tw.exitC, func() int64 {
return timeToMs(time.Now().UTC())
Expand All @@ -145,7 +152,9 @@ func (tw *TimingWheel) Start() {
select {
case elem := <-tw.queue.C:
b := elem.(*bucket)
rwlock.Lock()
tw.advanceClock(b.Expiration())
rwlock.Unlock()
b.Flush(tw.addOrRun)
case <-tw.exitC:
return
Expand Down