Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between net/rpc .Call vs .Go?

Tags:

go

rpc

I’m just getting started with Golang and the net/rpc package. I’m trying to understand when you might use the asynchronous client.Go() call over the client.Call() method most examples online use. Would calling client.Call asynchronously via something like

go client.Call(...)

essentially be the same as using the client.Go call? I’ve seen this example online too (for example when calling multiple simultaneous RPCs).

like image 712
aroooo Avatar asked Dec 06 '18 08:12

aroooo


People also ask

What is RPC in go?

Package rpc provides access to the exported methods of an object across a network or other I/O connection. A server registers an object, making it visible as a service with the name of the type of the object. After registration, exported methods of the object will be accessible remotely.

What is Gorpc?

Simple, fast and scalable golang RPC library for high load and microservices. Gorpc provides the following features useful for highly loaded projects with RPC: It minimizes the number of connect() syscalls by pipelining request and response messages over a single connection.


1 Answers

As documented:

Go invokes the function asynchronously. It returns the Call structure representing the invocation. The done channel will signal when the call is complete by returning the same Call object. If done is nil, Go will allocate a new channel. If non-nil, done must be buffered or Go will deliberately crash.

What this means is that it issues the command, but does not wait for it to finish.

By contrast:

Call invokes the named function, waits for it to complete, and returns its error status.

Neither method executes in a goroutine directly*--this is left as an exercise for the caller (so an argument might be made that Go is a misnomer).

If you look at the source to Call, perhaps it is more clear:

func (client *Client) Call(serviceMethod string, args interface{}, reply 
interface{}) error {
    call := <-client.Go(serviceMethod, args, reply, make(chan *Call, 1)).Done
    return call.Error
}

So in actuality, Call is a wrapper around Go, which waits for the operation to complete, whereas Go is the underlying function, which leaves waiting up to the caller.

*Clearly, in the background, a goroutine is involved somewhere, since this is a non-blocking operation.

like image 110
Flimzy Avatar answered Sep 23 '22 14:09

Flimzy