【问题标题】:http.Post() from a gRPC server to an http server returns EOF error on a docker-compose setup从 gRPC 服务器到 http 服务器的 http.Post() 在 docker-compose 设置上返回 EOF 错误
【发布时间】:2020-02-20 16:03:59
【问题描述】:

我有一个用 Go 编写的 gRPC 服务器 (server),Python gRPC 客户端 (client) 与之通信。服务器偶尔会向基于 Go 的 http 服务器 (sigsvc) 发送 http post 请求。所有这些实例都作为 docker 实例运行,通过 docker-compose 共享同一个 docker 网络。

这是 server 上创建和发送 http 请求的代码部分:

b := new(bytes.Buffer)
txbytes, err := json.Marshal(tx)
if err != nil {
    log.WithError(err).Error("failed to marshal transaction")
    return nil, err
}
b.Write(txbytes)

resp, err := http.Post(sigsvc.signerURL, "application/json; charset=utf-8", b)
if err != nil {
    log.WithError(err).Errorf("error signing transaction with signer %s", sigsvc.signerURL)
    return nil, err
}
defer resp.Body.Close()

var signedTx types.Transaction
err = json.NewDecoder(resp.Body).Decode(&signedTx)
if err != nil {
    log.WithError(err).Error("couldn't decode signed transaction")
    return nil, err
}

sigsvc.signerURL 映射到 http://signer:6666/sign 之类的东西,这是处理请求的 http 签名服务上的端点。 signer 指的是docker-compose.yml 规范中列出的服务名称。

这是处理程序在sigsvc 上的样子:

func (sv *SignerSv) handleSignTx() http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        log.Info("request received to sign transaction")
        dump, err := httputil.DumpRequest(r, true)
        if err != nil {
            http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
        }
        log.Debugf("%q", dump)

        if r.Body == nil {
            log.Error("request body missing")
            http.Error(w, "Please send a request body", 400)
            return
        }

        log.Debugf("request body: %v", r.Body)

        var tx types.Transaction
        err = json.NewDecoder(r.Body).Decode(&tx)
        if err != nil {
            log.WithError(err).Error("failed to unmarshal transaction")
            http.Error(w, err.Error(), 400)
            return
        }

        log.WithFields(log.Fields{
            "txhash":   tx.Hash().Hex(),
            "nonce":    tx.Nonce(),
            "to":       tx.To().Hex(),
            "data":     tx.Data(),
            "gasLimit": tx.Gas(),
            "gasPrice": tx.GasPrice(),
            "value":    tx.Value(),
        }).Debug("Decoded transaction from request body")

调试日志成功转储了请求和请求正文。但是,显然将请求正文解码为 transaction 类型的行永远不会执行,因为没有记录错误或解码的事务日志。

server 上,我不断收到以下错误: error="Post http://signer:6666/sign: EOF"

这是请求登录sigsvc的方式:

msg="\"POST /sign HTTP/1.1\\r\\nHost: signer:6666\\r\\nConnection: close\\r\\nAccept-Encoding: gzip\\r\\nConnection: close\\r\\nContent-Length: 10708\\r\\nUser-Agent: Go-http-client/1.1\\r\\n\\r\\n{\\\"nonce\\\":\\\"0x0\\\",\\\"gasPrice\\\":\\\"0x2540be400\\\",\\\"gas\\\":\\\"0x15c285\\\",\\\"to\\\":null,\\\"value\\\":\\\"0x0\\\",\\\"input\\\":\\\"0x6080604055",\\\"v\\\":\\\"0x0\\\",\\\"r\\\":\\\"0x0\\\",\\\"s\\\":\\\"0x0\\\",\\\"hash\\\":\\\"0xab55920fb3d490fc55ccd76a29dfb380f4f8a9e5d0bda4155a3b114fca26da0a\\\"}\"

我尝试在类似但简化的 docker 设置上重现此错误,但我失败了。

我试图理解以下内容:

  1. 如果此代码有任何问题,由于 到 docker 上的特定设置?
  2. 或者,我是否需要查看一些 docker 设置细节来调试实例。

【问题讨论】:

  • 您在编组到 txbytes 时忽略了该错误,该错误随后很可能为 nil 或空 - 这与 EOF 错误消息匹配。编组时请检查错误。
  • 此外,可以使用 json.Encoder 以 b 作为目标来完成对字节缓冲区的编组和写入。
  • 是的。此版本的代码在编组到 txbytes 时会忽略错误。话虽如此,在这种情况下,编组确实是成功的,因为我在sigsvc 上的 http 处理程序上获得了请求正文
  • 在将编组的错误处理部分添加到 txbytes 后,我可以确认我仍然收到此错误。
  • 在研究 Docker 网络之前,我会尝试两件事:1) 先尝试读取整个主体,然后使用 json.Unmarshal 而不是解码器,2) 尝试不调用 DumpRequest跨度>

标签: docker http go error-handling docker-compose


【解决方案1】:

问题在于 http 处理程序代码在此 logrus 调用中记录 to 字段的方式。

log.WithFields(log.Fields{
    "txhash":   tx.Hash().Hex(),
    "nonce":    tx.Nonce(),
    "to":       tx.To().Hex(),
    "data":     tx.Data(),
    "gasLimit": tx.Gas(),
    "gasPrice": tx.GasPrice(),
    "value":    tx.Value(),
}).Debug("Decoded transaction from request body")

在特定情况下,tx.To() 调用返回nil,这意味着调用tx.To().Hex() 会因为尝试对nil 指针进行方法调用而导致错误。从表面上看,人们会期望 log.WithFields() 调用出错或恐慌,但处理程序会默默地关闭与客户端的连接,以获得 EOF 响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-02
    • 1970-01-01
    • 2021-03-29
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多