Skip to content

t1k:nakama:grpc-client

FieldValue
Modulebase
Version1.6.2
Effortlow
Tools

Keywords: backoff, client, grpc, health check, microservice, nakama, reconnection

/t1k:nakama:grpc-client

The gRPC client in internal/grpcclient/ manages the connection to the Gameplay microservice with automatic reconnection using Fibonacci-backoff delays.

client, err := grpcclient.NewGameplayClient(ctx, logger, addr, apiKey)
  • Fibonacci backoff: Retry delays follow 1, 1, 2, 3, 5, 8… seconds
  • Health check: Verifies server is responding after connection
  • State monitoring: Watches connectivity.State changes, auto-reconnects on failure
  • IsConnected(): Check before every RPC call
  • GAMEPLAY_GRPC_ADDR — target address (default: gameplay:9550)
  • GAMEPLAY_API_KEY — service-to-service auth key sent as metadata
if !client.IsConnected() {
return "", runtime.NewError("gameplay service unavailable", 14)
}
resp, err := client.Method(ctx, req)
if err != nil {
return "", mapGRPCError(err)
}
  • Client starts connecting in a goroutine — don’t block InitModule waiting for connection
  • Health check runs after initial connect to verify the server is actually responding
  • If Gameplay service is down at startup, client retries indefinitely with backoff
  • API key is sent as gRPC metadata on every call — not as TLS client cert
  • Connection state changes are logged — check logs for “connectivity state changed”
  • Connection pooling: gRPC connections are multiplexed. Creating new connections per-request wastes resources and causes port exhaustion.
  • Fibonacci backoff ceiling: Cap backoff at 30 seconds. Uncapped backoff leads to minute-long delays after transient failures.
  • TLS cert validation: In production, always validate server certificates. Skip only in dev with explicit flag.
  • Client created in InitModule with 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