Search code examples
gorpc

Does RPC have a timeout mechanism?


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?


Solution

  • 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.