Checking if Service is registered via GetServiceInfo()

23 views
Skip to first unread message

neeraj....@gmail.com

unread,
Oct 30, 2018, 10:00:46 PM10/30/18
to grpc.io
Hi,
I was creating a library which allows creating the basic GRPC server for a client of the library and also running the grpc.Serve method on behalf of the client.
The client of the library provides a function value which performs the Service Registration function. This is passed to the library as a 
func(){ /* register service code */ }

However, I felt that before calling
grpc.Serve()

, I should check if the service has been registered by checking the output of
GetServiceInfo()

from the GRPC Server which is newly created. If this is nil, then return an error to the client of the library indicating that the service has not been registered and that the client needs to pass in a registration function (which is generated by the protoc compiler)

Is this a good idea to provide this sort of functionality/error handling when exposing GRPC boilerplate (such as creating a listener, GRPC server,etc.) via a custom library package ?

Regards,
N

Doug Fawley

unread,
Nov 7, 2018, 2:00:21 PM11/7/18
to grpc.io
I don't think you need to worry about whether the caller has properly registered a service, personally.  That would be the caller's responsibility.

An alternate way to make a library of this kind would be to have a type like this:

type Server struct {
  GRPC *grpc.Server
  lis net.Listener
}

func NewServer() *Server // initialize GRPC and lis appropriately
func (s *Server) Serve() error // call GRPC.Serve(lis)
func (s *Server) Close() error // stop GRPC and lis

With this model, the user would be responsible for making sure they register their services properly before calling Serve, and you wouldn't need to pass a lambda around.

Doug
Reply all
Reply to author
Forward
0 new messages