┌─────────────────────────────────────────────────────────────┐
│ Client Requests │
│ (HTTP/HTTPS/WebSocket) │
└────────────────────────┬────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Reverse Proxy (Main) │
│ Port: 8080 (configurable) │
│ - Request parsing │
│ - Route matching │
└────────────────┬────────────────────────────────────────────┘
│
┌────────┴─────────┬──────────────┬──────────────┐
│ │ │ │
▼ ▼ ▼ ▼
┌────────┐ ┌──────────────┐ ┌────────┐ ┌─────────┐
│ Router │ │ Middleware │ │ Cache │ │ Events │
│ │ │ Chain │ │ Layer │ │ System │
└────┬───┘ └──────────────┘ └────────┘ └─────────┘
│
└─────────────────┬─────────────────────────────────┐
│ │
┌──────▼──────┐ ┌──────────▼──┐
│ Backend │ │ Advanced │
│ Pool │ │ Features │
│ Manager │ │ │
└──────┬──────┘ └─────────────┘
│
┌─────────────────┼─────────────────┬────────────────┐
│ │ │ │
▼ ▼ ▼ ▼
┌─────────┐ ┌──────────────┐ ┌────────────┐ ┌──────────┐
│ Health │ │ Load │ │ Failover │ │ Rate │
│ Checker │ │ Balancing │ │ Logic │ │ Limiting │
└─────────┘ └──────────────┘ └────────────┘ └──────────┘
│
└─────────────────┬───────────────────────────────────┐
│ │
┌──────▼────────────────┐ ┌─────────▼──┐
│ Backend Servers │ │ Monitoring │
│ │ │ & Metrics │
│ • Health checking │ │ │
│ • Connection pooling │ └────────────┘
│ • Load distribution │
└─────────────────────┘
│
┌──────────────────────┼──────────────────────┐
│ │ │
▼ ▼ ▼
┌────────┐ ┌────────┐ ┌────────┐
│Service │ │Service │ │Service │
│ 1 │ │ 2 │ │ N │
└────────┘ └────────┘ └────────┘
- Purpose: Match incoming requests to routes
- Features:
- Path-based matching
- Subdomain-based matching
- Header-based matching
- Regex pattern matching
- Priority-based selection
Flow:
Request → Check Methods → Check Subdomain → Check Headers →
Check Path → Check Pattern → Return Matching Route
- Purpose: Manage backend servers
- Features:
- Round-robin load balancing
- Health status tracking
- Server pooling
- Automatic failover
Flow:
Get Server → Filter Healthy → Round-robin Selection → Return Server
- Purpose: Monitor backend health
- Features:
- Periodic health checks
- Configurable intervals
- Status updates
- Automatic recovery detection
Flow:
Interval Tick → Check Each Server → GET /health →
Status Code 200? → Update Status → Next Interval
- Purpose: Process requests through middleware
- Chain Order:
- Logging middleware
- CORS middleware
- Auth middleware
- Rate limiting (in handler)
Features:
- Extensible chain
- Request/response modification
- Early termination on error
- Purpose: Main request handling
- Features:
- Route matching
- Middleware execution
- Backend selection
- Request forwarding
- Response caching
- Event emission
Request Flow:
1. Rate Limit Check
├─ Exceeded? → Return 429
└─ Allowed? → Continue
2. Middleware Chain
├─ Logging
├─ CORS
├─ Auth
└─ Error? → Return Error
3. Route Matching
├─ Found? → Continue
└─ Not Found? → Return 404
4. Backend Selection
├─ Available? → Continue
└─ None Available? → Return 503
5. Cache Check
├─ Cache Hit? → Return Cached
└─ Cache Miss? → Continue
6. Forward Request
├─ Forward to Backend
├─ Get Response
├─ Cache Response (if applicable)
└─ Return to Client
7. Event Emission
└─ Emit appropriate events
User Request → Hash User ID → Calculate Variant Percent →
Select Variant A or B → Route Request → Track Metrics
Traffic Shift Start → Calculate Shift Percent →
Route Percent to New Version → Monitor Errors →
Complete Shift → Activate New Version
Closed (Normal) → Failure Threshold Reached → Open (Failed) →
Wait Timeout → Half-Open (Test) → Success? → Closed or Open
- Purpose: Load and manage configuration
- Formats:
- YAML (primary)
- JSON (alternative)
Structure:
Config
├── Server (Host, Port, TLS)
├── Backends (List of backend pools)
├── Routes (Routing rules)
└── Policies (Rate limit, CORS, Auth, Cache)
Client Request: GET /users/123
1. Router matches /users → users-service backend
2. Backend pool selects healthy user-service instance
3. Request forwarded to: http://users-service:3001/users/123
4. Response cached (if GET)
5. Response returned to client
6. Event: "request_forwarded" emitted
Client Request: GET / (from tenant1.app.com)
1. Router extracts subdomain: "tenant1"
2. Route matches subdomain: tenant1-route
3. Backend pool selected: tenant1-backend
4. Rate limiter checks per-tenant limit
5. Request forwarded to tenant1 service
6. Response returned
Client Request: GET /api/data
1. Blue-Green Manager checks traffic shift (20% to green)
2. Client hash: 45 (out of 100)
3. 45 > 20 → Route to Blue version
4. Request forwarded to blue backend
5. After 100%, all requests go to green
Client Request: GET /api/resource
1. Rate Limiter checks client IP: 10.0.0.1
2. Bucket for 10.0.0.1: 5 tokens remaining
3. Request uses 1 token
4. 4 tokens remaining
5. Request allowed → Continue
Later:
1. Bucket: 0 tokens
2. Next request arrives
3. 429 Too Many Requests returned
- Router: RWMutex for route list
- Backend Pool: Atomic operations for health status
- Rate Limiter: Map with per-client locks
- Cache: RWMutex for cache map
- Event System: Goroutines for async event handling
- Health Checker: Background goroutine checking health
- Event Handlers: Goroutines for each event listener
- Request Forwarding: Each request in separate goroutine
- Cache Cleanup: Background goroutine (optional)
- Route Matching: O(n) where n = number of routes
- Backend Selection: O(1) round-robin with filtering
- Rate Limit Check: O(1) map lookup + bucket calculation
- Cache Lookup: O(1) map access
- Cache: O(c) where c = number of cached entries
- Connection Pool: O(p) where p = max connections
- Backend Servers: O(b) where b = number of backends
Client → Proxy (Port 8080) → Multiple Backends
Client → LB → Proxy 1, Proxy 2, Proxy 3 → Backends
Client → Proxy 1 → Proxy 2 → Backend
(For additional filtering/transformation)
┌─→ Proxy (Users)
Client → LB
└─→ Proxy (Orders)
└─→ Proxy (Products)
┌──────────────┐
│ Request │
└──────┬───────┘
│
▼
┌──────────────────┐
│ Rate Limit │
│ (IP-based) │
└──────┬───────────┘
│
▼
┌──────────────────┐
│ CORS Policy │
│ (Origin check) │
└──────┬───────────┘
│
▼
┌──────────────────┐
│ Auth Validation │
│ (JWT/API Key) │
└──────┬───────────┘
│
▼
┌──────────────────┐
│ Route to Backend │
│ (Trusted) │
└──────────────────┘
See README.md for detailed documentation.