下面是直接可应用的实例:
//该接口超过3s 未处理完就报超市错误
clientDeadline := time.Now().Add(time.Duration(3000 * time.Millisecond))
ctx, cancel := context.WithDeadline(ctx, clientDeadline)
defer cancel()
res, err := xx.Getxxx(ctx, req)
if err != nil {
fmt.Println("=========== err:", res)
statu, ok := status.FromError(err)
if ok {
//判断是否为调用超时
if statu.Code() == codes.DeadlineExceeded {
fmt.Println("migration.UpdateProp timeout!")
}
}
}