【问题标题】:Golang: How to terminate/abort/cancel inbound http request without response?Golang:如何在没有响应的情况下终止/中止/取消入站 http 请求?
【发布时间】:2016-07-28 18:45:33
【问题描述】:

从服务器端,我需要终止/中止请求,而不需要像nginx's 444 这样的客户端做出任何响应。

从客户端看,它应该看起来像是被对等方重置的连接。

【问题讨论】:

    标签: go request no-response


    【解决方案1】:

    我花了几个小时才偶然发现http.Hijacker 允许从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()
        }
    }
    

    在某些情况下,终止连接可能有助于节省 CPU 时间和出站流量。

    【讨论】:

      猜你喜欢
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-02
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多