Chunked http response with go routines

90 views
Skip to first unread message

hasangenc...@gmail.com

unread,
Jul 15, 2019, 2:14:22 PM7/15/19
to golang-nuts
flusher, ok := w.(http.Flusher)

// w.wr is io.Writer here

for _, gateway := range app.Gateway {

 go func(w http.ResponseWriter) {
    // w.wr is nil here

tmpl, err := template.New(gateway.Name).Parse(gateway.Content)

if err != nil {
panic("An Error occured when parsing html")
return
}

tmpl.Execute(w, "")

defer flusher.Flush()
}()

}

Why w.wr is nil inside the function ? When i use function as non routine it works fine but i want to use the function as go routine.

Burak Serdar

unread,
Jul 15, 2019, 2:32:54 PM7/15/19
to hasangenc...@gmail.com, golang-nuts
This isn't valid code. The goroutine is declared with a w arg, but it
is not called with the parameter.

Assuming you intended to pass the w declared outside the for loop: is
it possible that the whole thing is inside another for-loop, and w is
the loop variable?

for _,w:=range ... {
// your code here
}

If that's the case, just add a w:=w at the top of the for loop:

for _,w:=range ... {
w:=w
// your code here
}

If that's not the case, maybe posting code that compiles might help.

>
> --
> 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 golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/f167b667-fb31-4c20-bf44-f6a114b21c62%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Hasan Genc

unread,
Jul 16, 2019, 2:34:10 AM7/16/19
to golang-nuts
Sorry, I made a mistake when copy paste :)
Actually we can assume above code as 
go func() {
 ...
}
Because I want to reference of w variable which is the type of http.ResponseWrite.
My actual problem is w.wr is nil inside go routine. I can post the code today but I can not access the code right now.

On Monday, July 15, 2019 at 9:32:54 PM UTC+3, Burak Serdar wrote:
> To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

Hasan Genc

unread,
Jul 16, 2019, 2:02:54 PM7/16/19
to golang-nuts
This is my original code.
outside of the go routine w variable is

Screen Shot 2019-07-16 at 20.57.35.png










But inside the go routine
Screen Shot 2019-07-16 at 20.58.17.png







I don't understand why. Anyone can help about this ? 


func Make(w http.ResponseWriter, r *http.Request, app App) {
runtime.GOMAXPROCS(4)

flusher, ok := w.(http.Flusher)

if !ok {
panic("expected http.ResponseWriter to be an http.Flusher")
}

w.Header().Set("X-Content-Type-Options", "nosniff")

tmpl, err := template.New("app").Parse(app.Page.Content)


if err != nil {
panic("An Error occured when parsing html")
return
}

   err = tmpl.Execute(w, "")

if err != nil {
panic("Error in Template.Execute")
}

flusher.Flush()


for _, gateway := range app.Gateway {

      go func(gateway Gateway) {

tmpl, err := template.New(gateway.Name).Parse(gateway.Content)

if err != nil {
panic("An Error occured when parsing html")
}

         err = tmpl.Execute(w, "")

if err != nil {
panic(err)
}

defer flusher.Flush()
}(gateway)
}

}

Jakob Borg

unread,
Jul 16, 2019, 2:14:10 PM7/16/19
to Hasan Genc, golang-nuts
At a guess, because your goroutine is running after the response handler has returned, and at that point the ResponseWriter isn’t valid any more. 

//jb

On 16 Jul 2019, at 20:03, Hasan Genc <hasangenc...@gmail.com> wrote:

This is my original code.
outside of the go routine w variable is









But inside the go routine

--
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 golang-nuts...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
<Screen Shot 2019-07-16 at 20.57.35.png>
<Screen Shot 2019-07-16 at 20.58.17.png>

Burak Serdar

unread,
Jul 16, 2019, 2:21:07 PM7/16/19
to Hasan Genc, golang-nuts
On Tue, Jul 16, 2019 at 12:03 PM Hasan Genc <hasangenc...@gmail.com> wrote:
This is my original code.

Multiple things are wrong with this code:

 * You are writing to a stream from multiple goroutines with no synchronization.
 * You are not waiting for the goroutines to complete before returning. Once the http handler returns, the response is written and the stream is closed.

If you really want to use goroutines for this task, process the templates in separate goroutines, collect the output via channels and write to the output stream from the handler's goroutine.
 
--
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 golang-nuts...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages