Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 704
digiadit Avatar asked Dec 10 '22 14:12

digiadit


1 Answers

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()
like image 198
Alexey Soshin Avatar answered Dec 13 '22 22:12

Alexey Soshin