Creating gRPC Servers
1. Creating Server Instance
Example: Basic server (Go)
srv := grpc.NewServer(
grpc.MaxRecvMsgSize(8<<20),
grpc.MaxSendMsgSize(8<<20),
)
| Option | Purpose |
|---|---|
grpc.Creds(tlsCreds) | Enable TLS |
grpc.UnaryInterceptor(...) | Single unary interceptor |
grpc.ChainUnaryInterceptor(...) | Multiple interceptors |
grpc.KeepaliveParams(...) | Server keep-alive config |
2. Registering Service Handlers
Example: Register implementation
type server struct { userv1.UnimplementedUserServiceServer }
func (s *server) GetUser(ctx context.Context, r *userv1.GetUserRequest) (*userv1.User, error) {
return &userv1.User{Id: r.Id}, nil
}
userv1.RegisterUserServiceServer(srv, &server{})
| Pattern | Detail |
|---|---|
Embed Unimplemented*Server | Forward compatible — new RPCs return Unimplemented |
| One impl per service | Pointer receiver, no shared state without sync |
3. Creating Network Listener
| Address | Use |
|---|---|
:50051 | All interfaces |
127.0.0.1:50051 | Localhost only |
unix:///tmp/grpc.sock | Unix domain socket |
4. Starting Server
| Method | Behavior |
|---|---|
Serve(lis) | Blocks until Stop() or fatal error |
Stop() | Immediate; cancels in-flight RPCs |
GracefulStop() | Drains active RPCs then stops |
5. Implementing Graceful Shutdown
Example: Signal-driven shutdown
go func() { _ = srv.Serve(lis) }()
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
done := make(chan struct{})
go func(){ srv.GracefulStop(); close(done) }()
select {
case <-done:
case <-ctx.Done(): srv.Stop()
}
| Step | Purpose |
|---|---|
| Catch SIGTERM | Kubernetes preStop signal |
| Drain timeout | Bound shutdown time |
Fallback to Stop() | Force kill if drain exceeds |
6. Handling Server Errors
| Source | Handling |
|---|---|
| Bind failure | Fatal — exit non-zero |
| Handler panic | Recovery interceptor → Internal |
| Stream error | Return status from handler |
7. Setting Server Options
| Option | Purpose |
|---|---|
MaxConcurrentStreams | Per-connection RPC cap |
ConnectionTimeout | Initial handshake timeout |
ReadBufferSize / WriteBufferSize | Tune throughput |
NumStreamWorkers | Worker pool size |
8. Configuring Max Message Size
| Default | Value |
|---|---|
| Server recv | 4 MB |
| Server send | Unlimited |
| Client recv | 4 MB |
| Client send | Unlimited |
Warning: If a single message exceeds the cap, the RPC fails with
ResourceExhausted. Use streaming for large payloads.9. Setting Connection Timeout
| Option | Detail |
|---|---|
grpc.ConnectionTimeout(d) | Max time for HTTP/2 handshake |
| Server keep-alive | See section 28 |
10. Enabling Server Reflection
| Purpose | Detail |
|---|---|
| Tool support | grpcurl, Postman, Evans |
| Production | Disable or gate behind auth |
11. Using Multiple Listeners
| Approach | Detail |
|---|---|
| cmux | Multiplex gRPC + HTTP on one port |
Multiple Serve() | Call once per listener in goroutines |
| Same server | One grpc.Server can serve many listeners |