diff --git a/game.go b/game.go index e9a2d88..e9f26de 100644 --- a/game.go +++ b/game.go @@ -87,8 +87,6 @@ func (g *Game) Win(winner *Connection, method string) { conn.Reset() } - ResetQueue() - g.Reset() for conn, _ := range g.connections { diff --git a/main.go b/main.go index e0b4943..1952bfb 100644 --- a/main.go +++ b/main.go @@ -98,7 +98,6 @@ func main() { if err != nil { bail(E_No_Port, "unable to start server: %v", err) } - go RunQueue() currentGame = NewGame() go currentGame.Run() diff --git a/work.go b/work.go deleted file mode 100644 index 3a5d367..0000000 --- a/work.go +++ /dev/null @@ -1,89 +0,0 @@ -package main - -import ( - "container/heap" - "sync" - "time" -) - -var ( - queue = make(Queue, 0, 32) - queueLock sync.Mutex -) - -type Future struct { - ts time.Time - index int - work func() -} - -type Queue []*Future - -func (q Queue) Len() int { return len(q) } - -func (q Queue) Less(i, j int) bool { - return q[i].ts.Before(q[j].ts) -} - -func (q Queue) Swap(i, j int) { - q[i], q[j] = q[j], q[i] - q[i].index = j - q[j].index = i -} - -func (q *Queue) Push(v interface{}) { - n := len(*q) - future := v.(*Future) - future.index = n - *q = append(*q, future) -} - -func (q *Queue) Pop() interface{} { - old := *q - n := len(old) - future := old[n-1] - future.index = -1 - *q = old[0 : n-1] - return future -} - -func At(ts time.Time, work func()) { - heap.Push(&queue, &Future{ts: ts, work: work}) -} - -func After(delay time.Duration, work func()) { - heap.Push(&queue, &Future{ts: time.Now().Add(delay), work: work}) -} - -func RunQueue() { - defer log_info("Queue runner done.") - heap.Init(&queue) - ticker := time.Tick(10 * time.Millisecond) - for { - select { - case <-ticker: - if queue.Len() == 0 { - break - } - future, ok := heap.Pop(&queue).(*Future) - if !ok { - log_error("there's shit on the work heap") - break - } - if future.ts.Before(time.Now()) { - future.work() - } else { - heap.Push(&queue, future) - } - } - } -} - -func ResetQueue() { - queueLock.Lock() - defer queueLock.Unlock() - - log_info("Reseting worker queue.") - queue = make(Queue, 0, 32) - heap.Init(&queue) -}