Does RPC have a timeout mechanism?

前端 未结 4 612
情书的邮戳
情书的邮戳 2021-02-09 14:30

If RPC does not have a timeout mechanism, how do I \"kill\" an RPC call if it is trying to call an RPC method of a server that is closed?

4条回答
  •  情书的邮戳
    2021-02-09 15:01

    You can use channels to implement a timeout pattern:

    import "time"
    
    c := make(chan error, 1)
    go func() { c <- client.Call("Service", args, &result) } ()
    select {
      case err := <-c:
        // use err and result
      case <-time.After(timeoutNanoseconds):
        // call timed out
    }
    

    The select will block until either client.Call returns or timeoutNanoseconds elapsed.

提交回复
热议问题