docker - 从 gRPC 服务器到 http 服务器的 http.Post() 在 docker-compose 设置上返回 EOF 错误

标签 docker http go error-handling docker-compose

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

这是服务器上创建并发送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 类型的行永远不会执行,因为没有记录错误或解码的事务日志。

服务器上,我不断收到以下错误: error="发布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 设置细节来调试实例。

最佳答案

问题出在 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() 调用会出错或出现 panic ,但处理程序会默默地关闭与获取 EOF 响应的客户端的连接。

关于docker - 从 gRPC 服务器到 http 服务器的 http.Post() 在 docker-compose 设置上返回 EOF 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58539261/

相关文章:

go - 如何从另一个函数返回函数?

networking - Docker 容器的多个静态 IP

bash - Docker-文件中基于动态正则表达式的sed替换

windows - docker-compose run 在 Windows 中不起作用

java - 当客户端使用 readTimeout 关闭连接时服务器会发生什么

http - 使用 OneToMany 响应资源上的创建操作的正确 HTTP 状态代码是什么

http - 登录和重定向 : What is the best HTTP flow for a webapp login?

docker - 如何在 docker run 命令中使用环境变量?

go - Go 中的 err.(*os.PathError) 是什么?

google-app-engine - 使用环境变量将值传递给 App Engine 上的 Go 应用程序