dqy27359 2017-08-16 16:26
浏览 69
已采纳

非常零星的Go HTTP错误:多个响应。WriteHeader调用

I wrote Kanali which is an open source Kubernetes Ingress/API management tool and for about 1/200k requests I receive the following fatal error:

2017/08/16 12:40:57 http: multiple response.WriteHeader calls
{"level":"error","method":"GET","msg":"unknown error","time":"2017-08-16T12:40:57Z","uri":"/ommitted/path"}
{"level":"fatal","msg":"write tcp4 192.168.2.160:8443-\u003e192.168.4.0:54554: write: broken pipe","time":"2017-08-16T12:40:57Z"}

I'm having a really hard time reproducing it but here is the relevant code. Kanali is a large project but the td;lr is that after this first code snippet is executed, the second code snipped is executed which handles errors.

func (step WriteResponseStep) Do(ctx context.Context, m *metrics.Metrics, c *controller.Controller, w http.ResponseWriter, r *http.Request, resp *http.Response, trace opentracing.Span) error {
    for k, v := range resp.Header {
        for _, value := range v {
            w.Header().Set(k, value)
        }
    }
    closer, str, err := utils.DupReaderAndString(resp.Body)
    if err != nil {
        logrus.Errorf("error copying response body, response may not be as expected: %s", err.Error())
    }
    trace.SetTag("http.status_code", resp.StatusCode)
    trace.SetTag("http.response_body", str)
    w.WriteHeader(resp.StatusCode)
    if _, err := io.Copy(w, closer); err != nil {
        return err
    }
    return nil
}

later in the code...

if err != nil {

    w.Header().Set("Content-Type", "application/json")

    switch e := err.(type) {
    case utils.Error:
        logrus.WithFields(logrus.Fields{
            "method": r.Method,
            "uri":    r.RequestURI,
        }).Error(e.Error())

        errStatus, err := json.Marshal(utils.JSONErr{Code: e.Status(), Msg: e.Error()})
        if err != nil {
            logrus.Warnf("could not marsah request headers into JSON - tracing data maybe not be as expected")
        }
        w.WriteHeader(e.Status())
        if err := json.NewEncoder(w).Encode(utils.JSONErr{Code: e.Status(), Msg: e.Error()}); err != nil {
            logrus.Fatal(err.Error())
        }
    default:
        logrus.WithFields(logrus.Fields{
            "method": r.Method,
            "uri":    r.RequestURI,
        }).Error("unknown error")
        errStatus, err := json.Marshal(utils.JSONErr{Code: http.StatusInternalServerError, Msg: "unknown error"})
        if err != nil {
            logrus.Warnf("could not marsah request headers into JSON - tracing data maybe not be as expected")
        }
        w.WriteHeader(http.StatusInternalServerError)
        if err := json.NewEncoder(w).Encode(utils.JSONErr{Code: http.StatusInternalServerError, Msg: "unknown error"}); err != nil {
            logrus.Fatal(err.Error())
        }
    }
}
  • 写回答

1条回答 默认 最新

  • drzip28288 2017-08-16 16:33
    关注

    The error message is telling you that WriteHeader is called more than once (either directly or indirectly by a call to Write). The header can only be written to the network once. Both snippets have a call to WriteHeader.

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

报告相同问题?

悬赏问题

  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致