-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide the implementation of the queue behind an interface and allow custom implementations to be used instead.
- Loading branch information
Showing
3 changed files
with
159 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package queue | ||
|
||
import ( | ||
"github.com/huandu/skiplist" | ||
"github.com/segmentio/ksuid" | ||
) | ||
|
||
func New() Queue { | ||
return Queue{ | ||
l: skiplist.New( | ||
skiplist.GreaterThanFunc(func(a, b interface{}) int { | ||
s1, s2 := a.(ksuid.KSUID).String(), b.(ksuid.KSUID).String() | ||
if s1 > s2 { | ||
return 1 | ||
} else if s1 < s2 { | ||
return -1 | ||
} | ||
return 0 | ||
}), | ||
), | ||
} | ||
} | ||
|
||
type Queue struct { | ||
l *skiplist.SkipList | ||
} | ||
|
||
func (q Queue) Set(id ksuid.KSUID, fn func()) (setAtFront bool) { | ||
e := q.l.Set(id, job{ID: id, Fn: fn}) | ||
return e.Prev() == nil | ||
} | ||
|
||
func (q Queue) Has(id ksuid.KSUID) bool { | ||
return q.l.Get(id) != nil | ||
} | ||
|
||
func (q Queue) Front() (ksuid.KSUID, func()) { | ||
if e := q.l.Front(); e != nil { | ||
v := e.Value.(job) | ||
return v.ID, v.Fn | ||
} | ||
return ksuid.KSUID{}, nil | ||
} | ||
|
||
func (q Queue) Remove(id ksuid.KSUID) (removed bool) { | ||
e := q.l.Remove(id) | ||
return e != nil | ||
} | ||
|
||
func (q Queue) Len() int { | ||
return q.l.Len() | ||
} | ||
|
||
func (q Queue) Scan( | ||
after ksuid.KSUID, | ||
fn func(ksuid.KSUID, func()) bool, | ||
) (afterFound bool) { | ||
var start *skiplist.Element | ||
var zero ksuid.KSUID | ||
if after != zero { | ||
if start = q.l.Get(after); start == nil { | ||
return false | ||
} | ||
start = start.Next() | ||
} else { | ||
start = q.l.Front() | ||
} | ||
|
||
for e := start; e != nil; e = e.Next() { | ||
j := e.Value.(job) | ||
if !fn(j.ID, j.Fn) { | ||
return true | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// job is a job descriptor. | ||
type job struct { | ||
ID ksuid.KSUID | ||
Fn func() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package sched | ||
|
||
import "time" | ||
|
||
type timeProvider struct{} | ||
|
||
func (p timeProvider) Now() Time { | ||
return time.Now() | ||
} | ||
|
||
func (p timeProvider) AfterFunc(d Duration, fn func()) Timer { | ||
return time.AfterFunc(d, fn) | ||
} |