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

从gRPC服务器到http服务器的http.Post()返回docker compose设置中的EOF错误,docker,http,go,error-handling,docker-compose,Docker,Http,Go,Error Handling,Docker Compose,我有一个用Go编写的gRPC服务器(server),pythongrpc客户机(client)与之对话。服务器偶尔向基于Go的http服务器发送http post请求(sigsvc)。所有这些实例都是通过共享同一docker网络的docker compose以docker实例的形式运行的 这是服务器上创建和发送http请求的代码部分: b := new(bytes.Buffer) txbytes, err := json.Marshal(tx) if err != nil { log.W

我有一个用Go编写的gRPC服务器(
server
),pythongrpc客户机(
client
)与之对话。服务器偶尔向基于Go的http服务器发送http post请求(
sigsvc
)。所有这些实例都是通过共享同一docker网络的docker compose以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")

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\\\"}\"
调试日志会成功转储请求和请求正文。但是,显然,将请求主体解码为
事务类型的行从未执行过,因为没有记录错误或解码的事务日志

服务器上
,我不断收到以下错误:
error=“Posthttp://signer:6666/sign: EOF“

这是请求登录到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")

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设置上重现此错误,但失败了

我试图理解以下几点:

  • 如果此代码有任何错误,并且由于 到docker上的特定设置
  • 或者,我是否需要查看一些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()
    调用会出错或死机,但处理程序会自动关闭与客户端的连接,以获得EOF响应。

    问题在于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响应。

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