Golang Http Server禁用Keep-Alive

时间:2016-10-14 10:37:27

标签: http go keep-alive

我试图在golang中禁用Keep-Alive Connection但是没有明确的解释如何做到这一点..

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))
}

任何人都可以解决这个问题吗?

1 个答案:

答案 0 :(得分:8)

SetKeepAlivesEnabled是一个实例配置,而不是全局配置 如果你真的需要实现它,请实例化你自己的服务器:

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

server.SetKeepAlivesEnabled(false)

server.ListenAndServe()