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?
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