Can gRPC be used for data entry?

I am wondering if it is worth transferring data from the gRPC server to the client. Basically, I want to use a pub / additional pattern with gRPC. What I am doing is that I am returning a stream of responses to the server implementation, which I never close. The client then has an endless startup routine to read that stream.

Here's an example:

service Service {
    rpc RegularChanges (Void) returns (stream Change) {}
}

      

Server side:

func (self *MyServiceImpl) RegularChanges(in *pb.Void, stream pb.Service_RegularChangesServer) error {

    for {
        d, err := time.ParseDuration("1s")
        if err != nil {
            log.Fatalf("Cannot parse duration")
            break;
        }
        time.Sleep(d)
        stream.Send(&pb.Change{Name:"toto", Description:"status changed"})
    }
    return nil
}

      

On the client:

for {
        change, err := streamChanges.Recv()
        if err != nil {
            log.Fatalf("Error retrieving change")
        } else {
            log.Println(change)
        }
}

      

I just started with go and gRPC, but I know this is HTTP2 based, so it should support pushing datas. However, I am not sure if this is the way to use gRPC.

+3


source to share


1 answer


gRPC is designed to be used this way.



You should still think about how the client should behave on failures and how you can rebalance the servers. If your connection goes over the Internet, you can also enable keepalive to detect connection failures by providing KeepaliveParams to the client and server.

+6


source







All Articles