In case someone arrives here with the same question I'm going to post a 
simple example that does what Tamás suggested a few days ago. Use context 
to block on .Done() and pass the responseWriter as a context Value. Please 
comment if passing the responseWriter as a context value is not considered 
a good practice.

package main

import (
"context"
"net/http"
"time"
)

var reqChan chan context.Context

func ConcPrinter(reqChan chan context.Context) {
for c := range reqChan {
w := c.Value("reqW").(http.ResponseWriter)
f := c.Value("cancel").(context.CancelFunc)
// Expensive operation
time.Sleep(time.Second)
w.Write([]byte("Done!"))
f()
}
}

func rootHandler(w http.ResponseWriter, r *http.Request) {
cont, f := context.WithCancel(context.WithValue(r.Context(), "reqW", w))
contC := context.WithValue(cont, "cancel", f)
reqChan <- contC
<-contC.Done()
}

func main() {
http.HandleFunc("/", rootHandler)
reqChan = make(chan context.Context)
go ConcPrinter(reqChan)
http.ListenAndServe(":8080", nil)
}

Cheers,
Pablo

On Wednesday, December 7, 2016 at 4:01:18 PM UTC-8, Pablo Rozas-Larraondo 
wrote:
>
> Hi gophers,
>
> I'm designing a service exposed through a http server which uses a pool of 
> workers to process requests. At the moment my handlerFunc look similar to 
> this:
>
> func aHandler(w http.ResponseWriter, r *http.Request) {
>         var wg sync.WaitGroup
>         wg.Add(1)
>         reqQueue <- &RequestParams{&wg, r, w}
>         wg.Wait()
> }
>
> When a worker is done, it writes on the w and calls wg.Done(). This 
> approach works fine but I'm not sure if passing the WaitGroup around is the 
> best approach. Has anyone encountered a similar situation or can suggest 
> alternative approaches? Could the new context package and its Done() 
> channel be of any help in this case? 
>
> Thank you very much for your help,
> Pablo
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to