【问题标题】:What can cause a client.Call rpc to return an error in Go (golang)?什么会导致 client.Call rpc 在 Go (golang) 中返回错误?
【发布时间】:2014-04-20 04:00:44
【问题描述】:

c.Call(...) 何时返回非零值?

c.Call(...) 能否仅在发生网络故障(数据包丢失或超时或类似情况)时返回错误?

如果服务器srv崩溃,c.Call(...)会返回错误吗?

具体来说,c.Call(...) 能否在请求成功到达srv 但在rpcname 处理函数返回之前返回错误?

import (
    "net/rpc"
    "fmt"
)

func call(srv string, rpcname string, args interface{}, reply interface{}) bool {
    c, errx := rpc.Dial("unix", srv)
    if errx != nil {
        return false
    }
    defer c.Close()

    err := c.Call(rpcname, args, reply)
    if err == nil {
        return true
    }

    fmt.Println(err)
    return false
}

【问题讨论】:

  • TFM 说:Call invokes the named function, waits for it to complete, and returns its error status. 这并没有具体说明除此之外会发生什么。如果由于网络错误、错误甚至恐慌导致执行确认无法到达客户端,可能会发生什么情况。

标签: go rpc


【解决方案1】:

如果您查看client.go in the source code for net/rpc,您会看到很多行设置了call.Error。这些应该显示Call 将返回错误的所有条件。

其中许多是在遇到来自ClientCodec.WriteRequestClientCodec.ReadResponseBody 的错误时生成的。有关详细信息,请参阅ClientCodec docs

遇到意外的 EOF 和ErrShutdown 客户端关闭时也有几个可能的错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-19
    • 2011-07-08
    相关资源
    最近更新 更多