英文:
What can cause a client.Call rpc to return an error in Go (golang)?
问题
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
}
英文:
When does <code>c.Call(...)</code> return a non-nil value?
Can the <code>c.Call(...)</code> only return an error when a network failure occurs (packets lost or timed out or something along those lines)?
If the server <code>srv</code> crashes, will <code>c.Call(...)</code> return an error?
Specifically, can <code>c.Call(...)</code> return an error AFTER the request successfully arrived at <code>srv</code> but BEFORE the <code>rpcname</code> handler function returns?
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
}
答案1
得分: 3
如果你查看一下net/rpc源代码中的client.go,你会看到很多设置call.Error的代码行。这些代码行展示了在哪些条件下Call会返回错误。
其中许多错误是在ClientCodec.WriteRequest和ClientCodec.ReadResponseBody遇到错误时生成的。请参阅ClientCodec文档了解更多详细信息。
还有一些可能的错误是在遇到意外的EOF时和客户端关闭时的ErrShutdown。
英文:
If you have a look at client.go in the source code for net/rpc you'll see quite a few lines where call.Error is set. These should show you all of the conditions under which a Call will return an error.
Many of them are generated upon encountering errors from ClientCodec.WriteRequest and ClientCodec.ReadResponseBody. See the ClientCodec docs for more details.
There are also a couple of possible errors for encountering unexpected EOF, and ErrShutdown when the client is closing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论