我有一个用Go语言编写的gRPC服务器(server
),Python gRPC客户端(client
)与之对话。该服务器偶尔会向基于Go的http服务器(sigsvc
)发送http发布请求。所有这些实例都作为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设置上重现此错误,但我失败了。
我试图了解以下内容:
问题出在http处理程序代码记录此to
调用中的logrus
字段的方式。
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响应。