1

http.server can be configure with these parameters:

ReadTimeout, WriteTimeout,IdleTimeout and others

I was just playing with parameters to see in which cases would be better to configure with SSE, so following the same structured from here I just add some prints to see when the clients conects and when disconects, like:

 ....
    notify := rw.(http.CloseNotifier).CloseNotify()
     fmt.Fprintf(w, "data: .\n\n") `//I add this because [onopen][3] won´t` fire if nothing is sent when start conecting
    fmt.Println(" conecting")
        for {
            select {
            case <-notify:
    fmt.Println("closing")
                return
            default:

                // Write to the ResponseWriter
                // Server Sent Events compatible
                fmt.Fprintf(rw, "data: %s\n\n", <-messageChan)

                // Flush the data immediatly instead of buffering it for later.
                flusher.Flush()
            }
        } 

So the only value that if I changed, it prints "closing" is WriteTimeout but reading this article it is confuse to me this part:

Finally, Go 1.8 introduces IdleTimeout which limits server-side the amount of time a Keep-Alive connection will be kept idle before being reused

But as I said even if I change IdleTimeout the client won't close, is it because the first time the client conects I write to it, or is it a bug? Which of three should I change then? Go version: 1.11.1

harold
  • 61,398
  • 6
  • 86
  • 164
John Balvin Arias
  • 2,632
  • 3
  • 26
  • 41
  • No special treatment is required to support SSE, for details and example, see [Why is Golang http.ResponseWriter execution being delayed?](https://stackoverflow.com/questions/48267616/why-is-golang-http-resposnsewriter-execution-being-delayed/48270817#48270817) Most likely your client closes the connection, not the server. Although intermediate gateways and proxies might close it if the connection is idle, so it's good practice to send "ping" messages periodically even if there is no data to send. – icza Oct 10 '18 at 12:36
  • One thing that might be missing from your solution: you have to set certain response headers before sending the first data frame, such as `ContentType=text/event-stream` and `Connection=keep-alive`. See the linked answer for details. – icza Oct 10 '18 at 14:00
  • actually indeed it needs configuration, as I said if you change "WriteTimeout" this is the time when the conection will get closed and the server won't send any data, you could try the same example from your link, and setting "WriteTimeout" to for example 10 seconds, you will see that the server won't send any data since 10 seconds have past, but what I do not know is why is this the case if "IdleTimeout" is supposed to handle this situation not WriteTimeout – John Balvin Arias Oct 11 '18 at 03:12
  • I'm sending the right headers – John Balvin Arias Oct 11 '18 at 03:12
  • This happens even sending the right headers – John Balvin Arias Oct 11 '18 at 03:23

0 Answers0