gRPC Server: Interceptor Not Running
In this tutorial, you'll learn about grpc server: interceptor not running. We cover key concepts, practical examples, and best practices.
gRPC server interceptors -- Register server-side interceptors in grpc.NewServer options to add logging, auth, and recovery.
The Problem
Interceptors must be passed as grpc.ServerOption. Adding them after server creation has no effect.
Wrong
server := grpc.NewServer()
pb.RegisterUserServer(server, &userServer{})
server.Serve(listener)
Output:
// No interceptors. Errors and panics go unhandled.
Right
server := grpc.NewServer(
grpc.ChainUnaryInterceptor(
logging.UnaryServerInterceptor(logger),
auth.UnaryServerInterceptor(validator),
recovery.UnaryServerInterceptor(),
),
)
pb.RegisterUserServer(server, &userServer{})
Output:
// Requests logged, authenticated, panics recovered.
Prevention
- Use ChainUnaryInterceptor to compose multiple interceptors
- StreamInterceptor for streaming RPCs
- Order: first registered runs first on request
- Common: Recovery -> Logging -> Auth -> Rate Limit
- Return status.Error for proper gRPC error codes
Common Mistakes with grpc server interceptor
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad
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