24

I was going through this code of gRPC server. Can anyone tell me the need for reflection used here

Code :

func main() {
    lis, err := net.Listen("tcp", port)
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    pb.RegisterGreeterServer(s, &server{})
    // Register reflection service on gRPC server.
    reflection.Register(s)
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

2 Answers 2

26

Server reflection is not necessary to run the helloworld example.

The helloworld example is also used as a server reflection example, that's why you see the reflection registering code there.

More about server reflection: Server reflection is a service defined to provides information about publicly-accessible gRPC services on a gRPC server. Tutorial available here: https://github.com/grpc/grpc-go/blob/master/Documentation/server-reflection-tutorial.md

2
  • reflection.Register(s) if err := s.Serve(lis); err != nil { log.Fatalf("failed to serve: %v", err) } Hence, I can safely remove this code, right?
    – Naresh
    Jan 4, 2017 at 8:31
  • Yes I write the sample myself and this line of code never comes to me. Feb 22 at 9:16
17

server-based-reflection is something that you will not need to build your day-to-day gRPC APIs.

This is a special instruction which exposes all the publicly accessible gRPC services on a gRPC server.
What this means essentially is that anyone can request your gRPC server to emit out details of the RPC service methods, request-response structures.

Where is this used?
This is used at places where you want to. dynamically call gRPC APIs. By dynamically I mean, the client does not need to hold the proto data-structures and register the RPC client stub.

1
  • So is registering reflection something that should be generally discouraged, due to any potential security reasons?
    – svakili
    Jan 31, 2022 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.