What can cause a client.Call rpc to return an error in Go (golang)?

huangapple go评论105阅读模式
英文:

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 (
    &quot;net/rpc&quot;
    &quot;fmt&quot;
)

func call(srv string, rpcname string, args interface{}, reply interface{}) bool {
    c, errx := rpc.Dial(&quot;unix&quot;, 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.WriteRequestClientCodec.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.

huangapple
  • 本文由 发表于 2014年3月15日 01:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/22412164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定