A high-performance, production-ready game gateway service implemented in Go, following industry best practices from Google, Netflix, and Uber. Designed for cloud-native environments with full observability, resilience, and scalability.
- Go 1.21 or later
- Kubernetes 1.20+ (for deployment)
- Redis (for dynamic routing configuration)
# Clone the repository
git clone https://github.com/SkynetNext/game-gateway.git
cd game-gateway
# Build
go build -o game-gateway ./cmd/gateway
# Run
./game-gateway -config config/config.yamldocker build -t game-gateway:latest .
docker run -p 8080:8080 -p 9090:9090 game-gateway:latestkubectl apply -f deploy/deployment.yaml
kubectl apply -f deploy/monitoring.yaml- Multi-Protocol Support: HTTP, WebSocket, and custom binary TCP protocol
- Session Management: High-performance sharded in-memory storage with O(1) access
- Connection Pooling: TCP connection reuse with configurable limits and idle timeout
- Dynamic Routing: Redis-based routing rules with multiple strategies (round-robin, consistent hash, realm-based)
- Protocol Forwarding: Bidirectional message forwarding with protocol translation
-
🛡️ Resilience
- Circuit Breaker: Automatic backend failure detection and recovery
- Retry Mechanism: Exponential backoff with configurable retries
- Graceful Shutdown: Drain mode with connection cleanup
- Health Checks:
/healthand/readyendpoints
-
🔒 Security
- Rate Limiting: Global and per-IP connection limits
- Input Validation: Message size limits, protocol validation
- DoS Protection: Configurable message size limits
-
📊 Observability
- Structured Logging: Zap-based JSON logging with Trace ID correlation
- Prometheus Metrics: Comprehensive metrics collection
- Distributed Tracing: OpenTelemetry support (Jaeger)
- Access Logging: Batched access logs with Trace ID
-
⚙️ Operations
- Configuration Hot Reload: Update settings without restart
- Kubernetes Integration: HPA support, Pod identification
- Configuration Validation: Fail-fast on invalid config
- Architecture: System architecture, components, and design decisions
- Performance: Performance characteristics, benchmarks, and tuning guide
- Logging Improvements: Distributed tracing and log correlation
- Trace Propagation: Trace ID propagation to backend services
┌─────────────┐
│ Clients │
└──────┬──────┘
│
▼
┌─────────────────────────────────────┐
│ Load Balancer (HAProxy) │
└──────┬──────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ Game Gateway Cluster (HPA) │
│ ┌──────────────────────────────┐ │
│ │ Session Management (Memory) │ │
│ │ Connection Pool (TCP) │ │
│ │ Routing Engine (Redis) │ │
│ │ Protocol Handler │ │
│ └──────────────────────────────┘ │
└──────┬──────────────────────────────┘
│
▼
┌─────────────────────────────────────┐
│ Backend Services (Cluster) │
│ • AccountServer │
│ • VersionServer │
│ • GameServer │
└─────────────────────────────────────┘
- Latency: P95 < 100ms, P99 < 200ms
- Throughput: 10,000+ connections per pod
- Availability: 99.9% uptime
- Error Rate: < 0.1%
game_gateway_connections_active- Active connectionsgame_gateway_sessions_active- Active sessionsgame_gateway_connection_setup_latency_seconds- Connection setup latency histogram (from TCP connection start to backend connection established)game_gateway_routing_errors_total- Routing errors by typegame_gateway_rate_limit_rejected_total- Rate limit rejectionsgame_gateway_circuit_breaker_state- Circuit breaker state
See Monitoring for complete metrics list.
server:
listen_addr: ":8080"
health_check_port: 9090
metrics_port: 9091
redis:
addr: "10.1.0.8:6379"
key_prefix: "game-gateway:"
routing:
refresh_interval: 10s
connection_pool:
max_connections: 1000
max_connections_per_service: 100
idle_timeout: 5m
security:
max_message_size: 1048576 # 1MB
max_connections_per_ip: 10
connection_rate_limit: 5
tracing:
enable_trace_propagation: false # Default: disabled for compatibilityLOG_LEVEL: Logging level (debug, info, warn, error) - default: "info"POD_NAME: Kubernetes Pod name (auto-injected)JAEGER_ENDPOINT: Jaeger collector endpoint (optional)
See Configuration Guide for details.
Pre-configured dashboard available in deploy/monitoring.yaml:
- Request rate and latency
- Connection and session metrics
- Error rates and circuit breaker status
- Resource utilization
Alert rules defined in deploy/prometheus-alerts.yaml:
- High error rate (> 1%)
- Circuit breaker open
- High connection count
- Rate limit hits
- High latency (P95 > 100ms)
- Pod failures
go test ./...go test -bench=. -benchmem ./...cd tools/loadtest
go build -o loadtest main.go
./loadtest -host localhost -port 8080 -connections 100 -duration 60s# Deploy gateway
kubectl apply -f deploy/deployment.yaml
# Deploy monitoring
kubectl apply -f deploy/monitoring.yaml
# Deploy alerts
kubectl apply -f deploy/prometheus-alerts.yamlIntegrated with Jenkins pipeline. See hgame_pipeline/Jenkinsfile for details.
game-gateway/
├── cmd/
│ └── gateway/ # Main application entry point
├── internal/
│ ├── gateway/ # Core gateway logic
│ ├── protocol/ # Protocol parsing and forwarding
│ ├── router/ # Routing engine
│ ├── pool/ # Connection pool
│ ├── session/ # Session management
│ ├── logger/ # Structured logging
│ ├── metrics/ # Prometheus metrics
│ ├── middleware/ # Access logging
│ └── tracing/ # OpenTelemetry tracing
├── config/ # Configuration files
├── deploy/ # Kubernetes manifests
├── tools/ # Utility tools
└── docs/ # Documentation
- Follow Go best practices and idioms
- Use structured logging with context
- Include unit tests for new features
- Document exported functions
- Run
gofmtandgolintbefore commit
- Connection Pool: O(1) operations, < 2μs per operation
- Session Management: O(1) access, < 1μs per operation
- Rate Limiter: < 200ns per check
- Message Forwarding: < 1ms latency (P95)
See Performance Guide for detailed benchmarks and tuning.
- High connection count: Check rate limiter settings, consider scaling
- Circuit breaker open: Check backend service health, review error logs
- Configuration refresh errors: Check Redis connectivity and network
- Memory leaks: Check connection pool cleanup, review session lifecycle
- High latency: Check backend response times, network conditions
- Rate limit rejections: Review rate limit settings, consider increasing limits
# Enable debug logging
LOG_LEVEL=debug ./game-gateway -config config/config.yaml
# View debug logs
kubectl logs -n hgame -l app=game-gateway --tail=100 -fQuery by Trace ID (Loki):
{app="game-gateway"} | json | trace_id="4bf92f3577b34da6a3ce929d0e0e4736"
Query access logs:
{app="game-gateway"} | json | msg="access_log" | status="error"
Query errors:
{app="game-gateway"} | json | level="error"
See Performance Guide - Troubleshooting for detailed troubleshooting steps.
- Documentation: See Documentation Index
- Issues: GitHub Issues
- Logs: Check application logs with Trace ID correlation
- Metrics: Review Prometheus metrics and Grafana dashboards
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
MIT License - see LICENSE file for details.
- Inspired by industry best practices from Google, Netflix, and Uber
- Built with zap for logging
- Uses OpenTelemetry for distributed tracing
- Issues: GitHub Issues
- Documentation: See
docs/directory - Performance: See PERFORMANCE.md
Built with ❤️ for high-performance game services