"balancer"
)
+// Anonymous struct fields, essentially struct emebedding.
+type reqResult struct {
+ *request
+ int
+}
+
+func (r reqResult) String() string {
+ return fmt.Sprintf("%v", r.int)
+}
+
type request struct {
reqFn func() int // what to compute for this particular request.
num int // Used for request identification.
- c chan int // channel for obtaining results back from worker.
+ c chan reqResult // channel for obtaining results back from worker.
}
func (r request) String() string {
func (r *request) Fn() interface{} {
fmt.Printf("%v: Fn()..\n", r)
- return interface{}(r.reqFn())
+ return interface{}(reqResult{r, r.reqFn()})
}
func (r *request) Send(res interface{}) {
- r.c <- res.(int)
+ r.c <- res.(reqResult)
}
-func furtherProcess(r *request, result int) {
- fmt.Printf("%v: further processing result %v..\n", r, result)
+func furtherProcess(r reqResult) {
+ fmt.Printf("%v: further processing result %v..\n", r.request, r.int)
}
func Generate(work chan<- balancer.Requester) {
- c := make(chan int)
+ c := make(chan reqResult)
reqNum := 0
for {
t := rand.Intn(5)
work <- req
result := <-c
fmt.Printf("Generate() '%v': received result %v\n", req, result)
- furtherProcess(req, result)
+ furtherProcess(result)
reqNum++
}
}