英文:
gRPC Unary Call Connection State Check on Server
问题
我有一个一元的 gRPC 调用,在我的 Go gRPC 服务器上可能需要几分钟才能处理(涉及移动应用程序上的人工代理)。我想知道在发送响应之前,是否有一种方法可以检查客户端是否已经终止了连接。
我找到了 ServerStreaming 情况下使用 Context Status.Done 通道的解决方案,但对于我的一元 RPC 来说,它不起作用。
以下是应该进行控制的函数的签名:
func (*Server) EndpointName(ctx context.Context, in *pb.EndpointRequest) (*pb.EndpointResponse, error) {
英文:
I have an unary gRPC call that can take up to few minute to be processed on my Go gRPC server (involves human agent on the mobile APP). I would like to know if there is a way to check if the connection has been terminated on the client side before sending the response.
I found the solution for ServerStreaming case with Context Status.Done channel, but it does not work for my Unary RPC.
Below is the signature of the function where the control should be made:
func (*Server) EndpointName(ctx context.Context, in *pb.EndpointRequest) (*pb.EndpointResponse, error) {
答案1
得分: 2
根据你的问题中显示的函数定义,端点函数接收一个context
(ctx context.Context
)。如果连接断开,上下文将被取消。
例如,我可以修改helloworld示例,以便模拟一个长时间运行的作业:
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
select {
case <-ctx.Done():
fmt.Println("上下文已完成:", ctx.Err())
return nil, status.Error(codes.Canceled, "无关紧要,因为任何东西都不会得到这个...")
case <-time.After(time.Minute):
// 这里模拟一个长时间运行的过程。实际上,该过程本身应该检查上下文
}
return &pb.HelloReply{}, nil
}
为了测试这个,我修改了greeter_client
来调用这个函数,并在等待响应时触发panic()
;服务器输出如下:
2022/08/08 08:16:57 服务器监听在 [::]:50051
上下文已完成:上下文已取消
英文:
As per the function definition shown in your question, the endpoint function is passed a context
(ctx context.Context
). If the connection drops the context will be cancelled.
For example I can modify the helloworld example so that it simulates a long running job:
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
select {
case <-ctx.Done():
fmt.Println("Context is done:", ctx.Err())
return nil, status.Error(codes.Canceled, "does not matter as nothing will ever get this anyway...")
case <-time.After(time.Minute):
// This simulates a long-running process. In reality the process itself should be checking the context
}
return &pb.HelloReply{}, nil
}
To test this I altered greeter_client
to call the function and then panic()
whilst waiting for the response; the server outputs:
2022/08/08 08:16:57 server listening at [::]:50051
Context is done: context canceled
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论