From de28dc142dac1fef68d2f83b0c45af54a218b038 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Tue, 11 Nov 2014 17:58:22 -0500 Subject: [PATCH] worker semantics change --- work.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/work.go b/work.go index f3bd0eb..3a5d367 100644 --- a/work.go +++ b/work.go @@ -58,22 +58,24 @@ func After(delay time.Duration, work func()) { func RunQueue() { defer log_info("Queue runner done.") heap.Init(&queue) + ticker := time.Tick(10 * time.Millisecond) for { - if len(queue) == 0 { - time.Sleep(10 * time.Microsecond) - continue + 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) + } } - queueLock.Lock() - future, ok := heap.Pop(&queue).(*Future) - queueLock.Unlock() - if !ok { - log_error("there's shit on the work heap") - continue - } - if future.ts.After(time.Now()) { - time.Sleep(future.ts.Sub(time.Now())) - } - future.work() } }