非常零散的Go HTTP错误:多次调用response.WriteHeader。

huangapple go评论91阅读模式
英文:

Very Sporadic Go HTTP Error: multiple response.WriteHeader calls

问题

我写了一个名为Kanali的开源Kubernetes Ingress/API管理工具,对于大约1/200k的请求,我收到了以下致命错误:

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->192.168.4.0:54554: write: broken pipe","time":"2017-08-16T12:40:57Z"}

我很难复现这个错误,但以下是相关的代码。Kanali是一个庞大的项目,但简而言之,执行第一个代码片段后,会执行处理错误的第二个代码片段。

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
}

代码的其他部分...

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())
        }
    }
}
英文:

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

得分: 3

错误消息告诉你 WriteHeader 被调用了多次(直接或间接通过 Write 调用)。头部只能写入网络一次。两个代码片段都调用了 WriteHeader。

英文:

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.

huangapple
  • 本文由 发表于 2017年8月17日 00:26:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/45718681.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定