Home >Backend Development >Golang >How Can I Implement Timeouts for RPC Calls?
Can RPC call requests be timed out?
RPC, or Remote Procedure Call, is a mechanism for calling functions in a different process, but due to network latencies or other issues, calls can sometimes get stuck.
If RPC does not have a built-in timeout mechanism, such situations can be handled by using 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 }
In this example:
The above is the detailed content of How Can I Implement Timeouts for RPC Calls?. For more information, please follow other related articles on the PHP Chinese website!