fullstorydev / grpchan

Channels for gRPC: custom transports
MIT License
204 stars 23 forks source link

Question: example on how to use inprocgrpc #65

Closed iqbalaydrus closed 2 years ago

iqbalaydrus commented 2 years ago

I've been digging the repo for a few hours and have no idea how to use the package. Also read the docs at https://pkg.go.dev/github.com/fullstorydev/grpchan@v1.1.1/inprocgrpc and still has no clue.

Server: https://github.com/grpc/grpc-go/blob/master/examples/helloworld/greeter_server/main.go

// server is used to implement helloworld.GreeterServer.
type server struct {
    pb.UnimplementedGreeterServer
}

// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
    log.Printf("Received: %v", in.GetName())
    return &pb.HelloReply{Message: "Hello " + in.GetName()}, nil
}

func main() {
    flag.Parse()
    lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    s := grpc.NewServer()
    // pb.RegisterGreeterServer(s, &server{})
        // and then what should I do next?
        inproc := &inprocgrpc.Channel{}
        pb.RegisterGreeterServer(inproc, &server{})
    log.Printf("server listening at %v", lis.Addr())
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

As for the client I absolutely has no clue how to use that. Can you give me an example on how to use inprocgrpc? Or direct me to some docs if available? Thank you

joeycumines commented 2 years ago

@iqbalaydrus inprocgrpc.Channel implements both grpc.ServiceRegistrar and grpc.ClientConnInterface.

In other words, you can use it directly with the generated stubs, e.g. https://pkg.go.dev/google.golang.org/grpc/examples/helloworld/helloworld#RegisterGreeterServer and https://pkg.go.dev/google.golang.org/grpc/examples/helloworld/helloworld#NewGreeterClient .

dragonsinth commented 2 years ago

@iqbalaydrus inprocgrpc.Channel is mostly used for:

a) writing test cases b) in the rare case that server needs a gRPC channel to itself

Most servers do not need an inproc channel. Are you trying to solve something specific?

iqbalaydrus commented 2 years ago

Ok, so I finally got it working. My usecase is we're building an application in a microservices manner via grpc, but can also be deployed only a single process. I was thinking to use inprocgrpc to have better performance compared to using localhost / in-memory transport if the application deployed to a single process. But weirdly it has slightly worse performance than the said alternatives, or maybe it's expected?

I'm closing this issue since the main question is answered.

dragonsinth commented 2 years ago

Interesting! I am surprised that inprocess channel would have worse performance than a literal TCP connection to the same process, but it's not impossible.

vominhtrius commented 1 year ago

@iqbalaydrus can you describe in more detail the performance of inprocgrc?