digiadit digiadit - 1 month ago 15
HTTP Question

Golang Http Server Disable Keep-Alive

I trying to disable Keep-Alive Connection in golang but there is no clear explanation about how to do it..

package main

import (
"net/http"
"github.com/julienschmidt/httprouter"
"fmt"
)

func helloworld(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Hello, World!")
}


func main() {

router := httprouter.New()

router.GET("/", helloworld)


fmt.Println("Running :-)")

http.Server.SetKeepAlivesEnabled(false)
log.Fatal(http.ListenAndServe(":3030", router))
}


can anybody solve this?

Answer

SetKeepAlivesEnabled is an instance configuration, not a global one.
If you really need to achieve that, instantiate your own server:

server := &http.Server{Addr: ":3030", Handler: router}

server.SetKeepAlivesEnabled(false)

server.ListenAndServe()
Comments