Skip to content

Instantly share code, notes, and snippets.

@RicardoLinck
Created June 25, 2020 21:52
Show Gist options
  • Save RicardoLinck/1167f15cf3993f485b67d8aca70ff149 to your computer and use it in GitHub Desktop.
Save RicardoLinck/1167f15cf3993f485b67d8aca70ff149 to your computer and use it in GitHub Desktop.
func greetWithTimeout(c greetpb.GreetServiceClient) {
req := &greetpb.GreetRequest{
Greeting: &greetpb.Greeting{
FirstName: "Ricardo",
LastName: "Linck",
},
}
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
res, err := c.Greet(ctx, req)
if err != nil {
grpcErr, ok := status.FromError(err)
if ok {
if grpcErr.Code() == codes.DeadlineExceeded {
log.Fatal("Deadline Exceeded")
}
}
log.Fatalf("Error calling Greet RPC: %v", err)
}
log.Printf("Response: %s\n", res.Result)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment