When making a go RPC call , the return type is channel

Viewed 300

Firstly, here is a PRC server. Please notice one of the return type is chan:

func (c *Coordinator) FetchTask() (*chan string, error) {
    // ...
    return &reply, nil
}

Then the client makes a RPC call. Typically the caller will get a channel which type is *chan string.

call("Coordinator.FecthTask", &args, &reply)

Here is my question. If the server continuously write into the channel:

for i := 0; i < 100; i++ {
    reply<- strconv.Itoa(i)
}

Can the client continuously read read from the channel?

for {
    var s string = <-reply
}

I guess the client can't, cuz server and client are not in the same memory. They communicate via Internet. Therefore, even the variable reply is a pointer, it points different address in server and client.

I'am not sure about it. What do you think of it? Thanks a lot!!!!

BTW, is there anyway to implement a REAL, stateful channel between server and client?

1 Answers

As you already mentioned, channels are in memory variables and it is not possible to use them in other apps or systems. In the other hand gRPC will pass and parse binary data which in this case again passing a channel pointer, will only returns the pointer address in server's memory. After client receiving that address it will try to point to that address in local machine's memory which will can be any sort of data unfortunately.

If you want to push a group of data (let's say an array of strings) you can use a Server streaming or Bidirectional streaming.

In the other hand if you want to accomplish some sort of stable and keep-alive connection you can consider websockets too.

Related