doupingpeng7567 2018-04-02 15:00
浏览 251

如何在GRPC中处理请求?

Lets say that my given GRPC endpoint has the following implementation

type HandlerFunc func(ctx context.Context, in *pb.Request) (*pb.Response, error)

func newHandler() HandlerFunc {
   func (s *Server) Process(ctx context.Context, in *pb.Request) (*pb.Response, error) {
        //do processing
    }
}

type Server struct {
    Handler handler
}

func new() (s *Server) {
   return &Server{
      handler: handlers.newHandler()
   }
}
func (s *Server) Process(ctx context.Context, in *pb.Request) (*pb.Response, error) {
    return s.handler
}

Now lets say that multiple clients try to call the Process endpoint. Do all of them call the same Process function of the same instance of the server or they call the Process function of the different instance of the server?

Also the handler defined in

func newHandler() HandlerFunc {
   func (s *Server) Process(ctx context.Context, in *pb.Request) (*pb.Response, error) {
        //do processing
    }
}

Is the same instance of the handler handling multiple requests from the same client? Also how about different clients? Is the same instance of the handler handling requests from multiple clients?

  • 写回答

1条回答 默认 最新

  • drsh30452 2018-04-10 23:54
    关注

    I'm sorry but I'm having a hard time understanding the code you provided. Seems like Process is the RPC call that you're writing a service handler for. But what is the handler type then. Also the function newHandler() is used as a method. Moreover, I don't see the need of wrapping the service handler either.

    How about we take the official example for unary RPCs from gRPC's github.

    If you look here, a gRPC service handler is defined which handles SayHello RPC.

    Multiple clients can make multiple calls to this RPC, all of which will be run in a separate goroutine. Now, it is understandable that one might want to share some information between these calls. This can be achieved by augmenting your service handler type server struct with more logic, for instance a data structure with a mutex.

    Hopefully, this answers some questions.

    Best Mak

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向