Согласно документу, сервер gRPC может проверять сроки как
if ctx.Err() == context.Canceled {
return status.New(codes.Canceled, "Client cancelled, abandoning.")
}
И я пытаюсь справиться с этим в примере привет
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
log.Printf("Received: %v", in.GetName())
select {
case <- time.After(time.Second):
// simulate some operation here
case <- ctx.Done():
if ctx.Err() == context.Canceled || ctx.Err() == context.DeadlineExceeded{
return nil, status.New(codes.Canceled, "Client cancelled, abandoning.").Err()
}
}
return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}
С кодами тестовых клиентов
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond*600)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
Мой вопрос: как проверить крайний срок в промежуточном программном обеспечении (или перехватчике) на сервере gRPC?
Что я пробовал
type serverStats struct {}
func (h *serverStats) TagRPC(ctx context.Context, info *stats.RPCTagInfo) context.Context {
fmt.Println("tag rpc")
return ctx
}
func (h *serverStats) HandleRPC(ctx context.Context, s stats.RPCStats) {
fmt.Println("handle rpc")
if ctx.Err() == context.Canceled || ctx.Err() == context.DeadlineExceeded {
fmt.Printf("HandleRPC: Client err %+v \n", ctx.Err())
}
}
...
s := grpc.NewServer(grpc.StatsHandler(&serverStats{}))
Однако HandleRPC
запускается несколько раз и НЕ может возвращать коды состояния ошибки.