使用gRPC推送数据是否可以?

I'm wondering if it's a good idea to push data from gRPC server to a client. Basically I want to use a pub/sub pattern with gRPC. The way I do is that I return a response stream on the server implementation that I never close. Then, the client has a never ending go routine in charge of reading this stream.

Here is an example:

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

On the 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 client:

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

I just began with go and gRPC but I know it's based on HTTP2, hence it should support pushing datas. However, I'm not sure this is the way gRPC should be used.

1个回答



gRPC旨在以这种方式使用。</ p>

您仍应考虑客户端的方式 应该表现出故障,以及如何在后端之间重新平衡。 如果您的连接是通过Internet进行的,则可能还需要通过向客户端和服务器提供KeepaliveParams来启用keepalive来检测连接中断。</ p>
</ div>

展开原文

原文

gRPC is intended to be used in this way.

You should still consider how the client should behave on failures and how you may want to re-balance across backends. If your connection is going across the Internet, you may also want to enable keepalive to detect connection breakages by providing KeepaliveParams to the client and server.

doupinyan0186
doupinyan0186 您是否有示例或示例实现的链接? :)
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐