t1k:nakama:grpc-client
| Field | Value |
|---|---|
| Module | base |
| Version | 1.11.11 |
| Effort | low |
| Tools | — |
Keywords: backoff, grpc, grpc client, health check, microservice, reconnection
How to invoke
Section titled “How to invoke”/t1k:nakama:grpc-clientNakama gRPC Client
Section titled “Nakama gRPC Client”Overview
Section titled “Overview”The gRPC client in internal/grpcclient/ manages the connection to the Gameplay microservice with automatic reconnection using Fibonacci-backoff delays.
Key Patterns
Section titled “Key Patterns”Client Initialization
Section titled “Client Initialization”client, err := grpcclient.NewGameplayClient(ctx, logger, addr, apiKey)Connection Management
Section titled “Connection Management”- Fibonacci backoff: Retry delays follow 1, 1, 2, 3, 5, 8… seconds
- Health check: Verifies server is responding after connection
- State monitoring: Watches
connectivity.Statechanges, auto-reconnects on failure - IsConnected(): Check before every RPC call
Configuration
Section titled “Configuration”GAMEPLAY_GRPC_ADDR— target address (default:gameplay:9550)GAMEPLAY_API_KEY— service-to-service auth key sent as metadata
Error Handling
Section titled “Error Handling”if !client.IsConnected() { return "", runtime.NewError("gameplay service unavailable", 14)}resp, err := client.Method(ctx, req)if err != nil { return "", mapGRPCError(err)}Hard rules
Section titled “Hard rules”- API key is sent as gRPC metadata on every call — not as TLS client cert
- TLS cert validation: In production, always validate server certificates. Skip only in dev with explicit flag.
Checklist
Section titled “Checklist”- Client created in
InitModulewith correct address and API key -
IsConnected()checked before every gRPC call - gRPC errors mapped via
mapGRPCError()to Nakama error codes - Connection timeout is reasonable for the deployment environment
- Health check endpoint exists on the Gameplay service