gRPC Deadline: Context Timeout Not Passed
In this tutorial, you'll learn about grpc deadline: context timeout not passed. We cover key concepts, practical examples, and best practices.
gRPC deadline propagation -- Set context deadlines on gRPC client calls and enforce them on the server.
The Problem
gRPC client calls without deadline wait indefinitely. Servers should respect the incoming deadline to avoid wasted work.
Wrong
resp, err := client.GetUser(context.Background(), req)
Output:
// Hangs if server is slow or unresponsive.
Right
// Client:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := client.GetUser(ctx, req)
// Server checks deadline:
func (s *server) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.User, error) {
deadline, ok := ctx.Deadline()
if !ok {
return nil, status.Error(codes.Internal, "deadline required")
}
if time.Until(deadline) < time.Second {
return nil, status.Error(codes.DeadlineExceeded, "too late")
}
// Do work...
}
Output:
// Client fails fast after 5s. Server rejects calls with insufficient time.
Prevention
- Always set deadline on gRPC client calls
- Servers can check ctx.Deadline() and ctx.Err()
- Deadline propagates from client to server automatically
- Set generous deadline (10-30s) for most RPCs
- Use shorter deadline for real-time RPCs
Common Mistakes with grpc deadline
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists
These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro