2

I have a gRPC service simmilar to below

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

I need the client to maintain a long living gRPC connection to the server so that if the server goes down, the client can reconnect and issue SayHello() again.

Based on my understanding there are a few options:

  1. Pass in a statsHandler to grpc.Dial and add retry logic in HandleConn()
  2. Add a new ClientStreaming API that maybe sends a message every few seconds. Check for server side stream close errors and implement retry logic.

Not sure if there is a recommended way for my use case and would appreciate any help.

  • You should be able to track the state of your connection. Check `GetState`, `WaitForStateChange`, `WaitForReady` functions in https://pkg.go.dev/google.golang.org/grpc. Also, you can check how that works here: https://stackoverflow.com/questions/66353603/correct-way-to-perform-a-reconnect-with-grpc-client/66359526#66359526 – Emin Laletovic Jan 26 '23 at 16:24
  • Thank you for the pointers. I was able to create a long-lived gRPC connection using the keep-alive mechanism in gRPC and also detect connection drops via GetState() and WaitForStateChange() – user2819943 Jan 30 '23 at 20:53

0 Answers0