return fmt.Sprintf("W %.2d (%v)", w.num, w.pending)
}
-func (w *Worker) work(done chan<- *Done) {
+func (w *Worker) work(id string, done chan<- *Done) {
t := rand.Intn(5)
- fmt.Printf("%v: sleeping for %v secs before begin..\n", w, t)
+ fmt.Printf(" %v [%v]: sleeping for %v secs before begin..\n", w, id, t)
time.Sleep(time.Duration(t) * time.Second)
+ timeout := time.After(time.Second)
for {
- req := <- w.requests
- fmt.Printf("%v: start computing '%v'\n", w, req)
- req.c <- req.fn()
- done <- &Done{w, req}
- fmt.Printf("%v: finished '%v'\n", w, req)
+ select {
+ case req := <- w.requests:
+ fmt.Printf(" %v [%v]: start computing '%v'\n", w, id, req)
+ req.c <- req.fn()
+ done <- &Done{w, req}
+ fmt.Printf(" %v [%v]: finished '%v'\n", w, id, req)
+ case <-timeout:
+ fmt.Printf(" %v [%v]: i'm stil here..\n", w, id)
+ timeout = time.After(time.Second)
+ }
}
}
func (b *Balancer) dispatch(req *Request) {
w := heap.Pop(&b.pool).(*Worker)
fmt.Printf("dispatch() '%v': sending to '%v'\n", req, w)
- go w.work(b.done)
+ go w.work(time.Now().Format("05.000"), b.done)
w.pending++
heap.Push(&b.pool, w)
w.requests <- req