I am running a grpc
server listening on localhost:6000
, exposing 2 grpc
services: RegisterSubscriberServiceServer
and RegisterDropperServiceServer
. Since both of these services are reachable from localhost:6000
, I'd like to only dial this address from the stub.
The server looks like this:
func main() {
grpcServer := grpc.NewServer()
pb.RegisterSubscriberServiceServer(grpcServer, &subscriberServer{})
pb.RegisterDropperServiceServer(grpcServer, &dropperServer{})
l, err := net.Listen("tcp", ":6000")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
log.Println("Listening on tcp://localhost:6000")
grpcServer.Serve(l)
}
Why do I need to dial a different socket for each grpc
service?
type DropperRPC struct {
conn *grpc.ClientConn
client pb.DropperServiceClient
chunkSize int
}
type SubscriberRPC struct {
conn *grpc.ClientConn
client pb.SubscriberServiceClient
chunkSize int
}
func NewSubscriber() (c SubscriberRPC, err error) {
c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c.client = pb.NewSubscriberServiceClient(c.conn)
return
}
func NewDropper() (c DropperRPC, err error) {
c.conn, err = grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c.client = pb.NewDropperServiceClient(c.conn)
return
}
And since the code is basically duplicated to accommodate each service, can't I just use an Interface
to reduce the code?
func main() {
c1, err := NewSubscriber()
if err != nil {
log.Fatal(err)
}
c2, err := NewDropper()
if err != nil {
log.Fatal(err)
}
cc1 := &c1
cc2 := &c2
}
And then use this Interface
to implement the client side grpc
functions for each service, instead of creating a new struct
for each service. I have found cmux , but there must be a way to do this without using external libraries.
Why do I need to dial a different socket for each grpc service?
You don't. You can create one grpc.ClientConn
and pass it to multiple pb.New*Client()
functions, and they will share the same connection(s).
func main() {
cc, err := grpc.Dial("localhost:6000", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
c1 := pb.NewSubscriberServiceClient(cc)
c2 := pb.NewDropperServiceClient(cc)
}
And then use this Interface to implement the client side grpc functions for each service, instead of creating a new struct for each service
The generated code in the pb.go
file does everything you need to perform RPCs. You shouldn't have to implement anything client-side unless you have special logic you want to happen automatically every time you perform a call.
If the two services have unique method names, you could put them into the same struct so you don't have to use them separately, as a minor convenience:
type SubscriberDropper struct {
pb.SubscriberServiceClient
pb.DropperServiceClient
}
func main() {
// ... as above ...
sd := &SubscriberDropper{c1, c2}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With