douzha6055 2016-04-07 17:54
浏览 339
已采纳

Golang:如何终止/中止/取消入站HTTP请求而无响应?

From server side I need to terminate/abort request without any response to client like nginx's 444.

From client side it should look like connection reset by peer.

  • 写回答

1条回答 默认 最新

  • doutanggun9816 2016-04-07 17:54
    关注

    I've spent a couple hours and only accidentally find http.Hijacker which allows to get access to net connection from http.ResponseWriter:

    h := func(w http.ResponseWriter, r *http.Request) {
        if wr, ok := w.(http.Hijacker); ok {
            conn, _, err := wr.Hijack()
            if err != nil {
               fmt.Fprint(w, err)
            }
            conn.Close()
        }
    }
    

    Terminating connection may be useful in some cases for saving CPU time and outbound traffic.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?