func Generate(work chan<- balancer.Requester) {
c := make(chan reqResult)
reqNum := 0
+ t := rand.Intn(5)
+ fmt.Printf("Generate(): sleeping for %v secs\n", t)
+ timeout := time.After(time.Duration(t) * time.Second)
for {
- t := rand.Intn(5)
- fmt.Printf("Generate(): sleeping for %v secs\n", t)
- time.Sleep(time.Duration(t) * time.Second)
+ select {
+ case <- timeout:
+ req := &request{num: reqNum, c: c}
+ f := func () int { fmt.Printf("... computing %v\n", req); return req.num }
+ req.reqFn = f
+ fmt.Printf("Generate(): sending '%v'\n", req)
+ work <- req
- req := &request{num: reqNum, c: c}
- f := func () int { fmt.Printf("... computing %v\n", req); return reqNum }
- req.reqFn = f
- fmt.Printf("Generate() '%v': sending\n", req)
- work <- req
- result := <-c
- fmt.Printf("Generate() '%v': received result %v\n", req, result)
- furtherProcess(result)
- reqNum++
+ t := rand.Intn(5)
+ fmt.Printf("Generate(): sleeping for %v secs\n", t)
+ timeout = time.After(time.Duration(t) * time.Second)
+ reqNum++
+ case result := <-c:
+ fmt.Printf("Generate(): received '%v' result %v\n", result.request, result.int)
+ furtherProcess(result)
+ }
}
}