This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
ConnX is a high-performance HTTP reverse proxy and load balancer written in Go that uses the gnet framework for efficient network operations. The project provides multiple load balancing algorithms, circuit breaker pattern, rate limiting, health checking, graceful shutdown, hot reload configuration, and real-time backend monitoring.
make build- Build the proxy binary tobin/proxymake test- Run all tests usinggo test ./...make run- Run the proxy directly withgo run cmd/proxy/main.gomake clean- Remove the bin/ directorygo run cmd/proxy/main.go -config=path/to/config.yaml- Run with custom config
go test ./...- Run all testsgo test -v ./internal/proxy- Run tests for specific package with verbose outputgo test -race ./...- Run tests with race detection
- Check for golangci-lint configuration for linting standards
- The project uses standard Go formatting with
go fmt
The application follows a clean architecture pattern with these key packages:
- cmd/proxy/main.go - Entry point that loads configuration, starts the server, handles graceful shutdown, and monitors config changes
- internal/server - Main gnet-based HTTP server that handles incoming connections and
/metricsendpoint - internal/server/reload.go - Hot reload logic for dynamic configuration updates without restart
- internal/proxy - Backend management and server pool operations with circuit breaker integration
- internal/health - Health checking system for backend servers with metrics integration
- internal/loadbalancer - Load balancing algorithms (round-robin, weighted round-robin, least connections)
- internal/circuitbreaker - Circuit breaker pattern implementation for automatic failure handling
- internal/ratelimit - Token bucket rate limiting (global and per-IP)
- internal/config - Configuration loading, validation, and file watching for hot reload
- internal/metrics - Prometheus-style metrics collection and exposition
- pkg/ - Reusable utilities (HTTP parsing, logging)
- Event-driven networking: Uses gnet.BuiltinEventEngine for high-performance connection handling
- Server pool pattern: Centralized management of backend servers with health status tracking
- Configuration-driven: YAML-based configuration with sensible defaults
- Graceful error handling: Proper error propagation and backend failure handling
- gnet server receives connection in
server.OnTraffic() - HTTP request is parsed from raw bytes
- Check if request is for
/metricsendpoint and handle accordingly - Load balancer selects next available backend via
pool.GetNextPeer() - Request is forwarded to backend with proper header copying
- Response is dumped and written back to client
- Metrics are recorded (request counts, response times, errors)
- Errors increment backend error counters and metrics
Configuration is loaded from YAML with the following structure:
server.port/server.host/server.shutdownTimeout- Server configurationbackends[]- List of backend configurations with URL and weightloadBalancing.algorithm- Load balancing algorithm selectionhealthCheck.interval/healthCheck.timeout- Health check parameterscircuitBreaker.*- Circuit breaker configurationrateLimit.*- Rate limiting configuration
Default values are applied in config.Validate() method:
- Port: 8080
- Host: "0.0.0.0"
- Shutdown timeout: 30 seconds
- Health check interval: 30 seconds
- Health check timeout: 5 seconds
- Load balancing: round-robin
- Circuit breaker: enabled with 5 max failures, 60s timeout
- Rate limit: disabled
- Runs in background goroutine with configurable intervals
- Performs HTTP GET requests to backend URLs
- Updates backend status in server pool and metrics
- Failed backends are marked unavailable and excluded from load balancing
- Endpoint:
/metrics- Prometheus-style metrics exposition - Metrics collected:
connx_requests_total- Total HTTP requests by method and statusconnx_errors_total- Total error countconnx_response_time_seconds- Response time statistics (avg, p95, p99)connx_backend_up- Backend health status (1=up, 0=down)connx_active_connections- Current active connectionsconnx_uptime_seconds- Server uptimeconnx_requests_per_second- Current RPS
- Integration: Metrics are collected in real-time during request processing
- Memory management: Response time history limited to last 1000 requests
- gnet v2.7.2 - High-performance networking framework
- yaml.v3 - YAML configuration parsing
- Standard library - HTTP utilities, logging, time operations
- The server uses multicore processing via
gnet.WithMulticore(true) - Port reusing is enabled for better performance
- Error counting is implemented per backend for monitoring
- Raw HTTP request/response handling for maximum performance
- Comments are now in English throughout the codebase
- ✅ Metrics endpoint (
/metrics) - Prometheus-style metrics for requests/sec, response times, error rates - ✅ Circuit breaker pattern - Automatically opens circuit after failures, prevents cascade failures
- ✅ Active connection tracking - Tracks active connections per backend for least-connections algorithm
- ✅ Multiple load balancing algorithms - Round-robin, weighted round-robin, least connections
- ✅ Weighted backends - Distribute traffic based on backend capacity/priority
- ✅ Dynamic backend updates - Add/remove/update backends without restart
- ✅ Rate limiting - Token bucket rate limiting with global and per-IP modes
- ✅ Circuit breaker - Automatic failure detection and recovery with half-open state
- ✅ Graceful shutdown - Proper connection draining on SIGTERM/SIGINT with configurable timeout
- ✅ Hot reload - Dynamic configuration updates without restart
- ✅ Automatic file watching - Detects config changes and applies them in real-time
- ✅ Zero-downtime updates - Change backends, algorithms, and settings without dropping connections
- ✅ Comprehensive configuration - All features configurable via YAML
- Connection pooling - Reuse HTTP connections to backends instead of creating new ones per request
- Request/response compression - Add gzip support for bandwidth optimization
- Path-based routing - Route requests to different backend pools based on URL paths
- Header-based routing - Route based on custom headers (e.g., API version, tenant ID)
- Sticky sessions - Session affinity using cookies or IP-based routing
- IP hash algorithm - Consistent hashing for request distribution
- TLS/HTTPS support - SSL termination and backend HTTPS connections
- Authentication middleware - JWT validation, API key checking
- Request/response transformation - Header manipulation, request/response body modification
- Structured logging - JSON logs with correlation IDs, request tracing
- Health check dashboard - Web UI showing backend status and statistics
- Hot configuration reload - Update backends/config without restart
- WebSocket proxying - Support for WebSocket connections
- HTTP/2 support - Modern protocol support for better performance
- Dynamic backend discovery - Integration with service discovery (Consul, etcd)
- Request caching - Cache responses for improved performance