HTTP Server - Force close connection after response

6,273 views
Skip to first unread message

Tony Grosinger

unread,
Jan 16, 2017, 10:36:27 PM1/16/17
to golang-nuts

I would like to create an HTTP server which forces the connection to be closed after writing the response.
For example:

func closingHandler(w http.ResponseWriter, r *http.Request) {
    // Respond before hijacking?
    fmt.Fprintf(w, "Hello World")

    hj, ok := w.(http.Hijacker)
    if !ok {
        log.Println("Unable to create hijacker")
        return
    }

    conn, bufrw, err := hj.Hijack()
    if err != nil {
        log.Println("Unable to hijack request")
        return
    }

    bufrw.Flush()
    conn.Close()
}

func main() {
    mux := http.NewServeMux()
    mux.HandleFunc("/", closingHandler)

    go func() {
        err := http.ListenAndServe(":8080", mux)
        if err != nil {
            log.Println("Failed to run server"
        }
    }()
}

This example does not work however because the client issuing the request cannot successfully read the response. Is there another way to close the connection manually that would allow me to respond to the HTTP request first?

Sairam Kunala

unread,
Jan 17, 2017, 12:03:37 AM1/17/17
to Tony Grosinger, golang-nuts
You could respond with "Connection: close" header

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tony Grosinger

unread,
Jan 17, 2017, 12:14:54 AM1/17/17
to golang-nuts
Isn't that just asking the client to close the connection? I need to not trust the client and force the connection to close.

Rodolfo Azevedo

unread,
Jan 17, 2017, 2:28:19 AM1/17/17
to golang-nuts
You can use defer:

defer bufrw.Flush()
defer conn
.Close()


It will close after method finishs, but I do not know if it will work for you because you using go routines to start server, I never see this, I always use:

log.Fatal(http.ListenAndServe(":8080", mux))


Well, I think you can try.

James Bardin

unread,
Jan 17, 2017, 7:06:44 AM1/17/17
to golang-nuts
No, it signals to the client that the connection will be closed after the completion of the response.

cat2...@gmail.com

unread,
Jan 17, 2017, 8:48:28 AM1/17/17
to golang-nuts
I think Calling https://golang.org/pkg/net/http/#Server.SetKeepAlivesEnabled given false should work what you want.
(You would need to use https://golang.org/pkg/net/http/#Server instead of using ServeMux directly)


2017年1月17日火曜日 12時36分27秒 UTC+9 Tony Grosinger:

James Bardin

unread,
Jan 17, 2017, 9:58:36 AM1/17/17
to golang-nuts
Sorry, google groups UI doesn't make it clear that this was in response to Tony's last message.

`Connection: close` tells the client that the connection *will be closed*, it doesn't request that the client close the connection. Just set `Connection: close` and the server will do the right thing.
Reply all
Reply to author
Forward
0 new messages