github.com/jeffjen/go-libkv@v0.0.0-20151212051932-5df59a45a168/timer/priority.go (about) 1 package timer 2 3 import ( 4 "time" 5 ) 6 7 // ticket is job metadata. 8 type ticket struct { 9 iden int64 `desc: uniqe identifier for a job` 10 pos int `desc: position in the priority queue` 11 12 a time.Time `desc: the designated time to fire Handler` 13 h Handler `desc: registered handler` 14 15 r bool `desc: flag for testing whether this is a recurring ticket` 16 d time.Duration `desc: the interval at which we repeat this ticket` 17 concurr int `desc: the maximum number of concurrent task to run` 18 ntask int `desc: the number of task running right now` 19 } 20 21 // priority is how we manage scheduling in Timer 22 type priority []*ticket 23 24 func (pri priority) Len() int { return len(pri) } 25 26 func (pri priority) Less(i, j int) bool { 27 return pri[i].a.Before(pri[j].a) 28 } 29 30 func (pri priority) Swap(i, j int) { 31 pri[j], pri[i] = pri[i], pri[j] 32 pri[i].pos = i 33 pri[j].pos = j 34 } 35 36 func (pri *priority) Push(x interface{}) { 37 n := len(*pri) 38 t := x.(*ticket) 39 t.pos = n 40 *pri = append(*pri, t) 41 } 42 43 func (pri *priority) Pop() interface{} { 44 old, n := *pri, len(*pri) 45 t := old[n-1] 46 t.pos = -1 // invalidate 47 *pri = old[0 : n-1] 48 return t 49 }