Traefik WebSocket Connection Balancer
A high-performance WebSocket connection balancer middleware for Traefik that distributes WebSocket connections across multiple backend pods based on their active connection count. This plugin is designed to ensure optimal load distribution for WebSocket-heavy applications.
Features
Quick Start
Prerequisites
Go 1.22 or higher
gorilla/websocket v1.5.3 or higher
Installation
Clone the repository:
Copy git clone https://github.com/yourusername/traefikwsbalancer.git
cd traefikwsbalancer
Install dependencies:
Build the project:
Basic Usage
Start the test server (provides backend WebSocket endpoints):
Copy go run cmd/test-server/main.go
In a separate terminal, run the test client:
Copy go run cmd/test-client/main.go
Configuration
Core Components
The balancer consists of three main components:
WebSocket Balancer (main middleware)
Connection Fetcher (metrics collector)
WebSocket Relay (message forwarder)
Configuration Options
Copy type Config struct {
MetricPath string
services [ ] string
TLSVerify bool
CacheTTL time . Duration
DialTimeout time . Duration
WriteTimeout time . Duration
ReadTimeout time . Duration
}
Default Values
MetricPath: "/metric"
TLSVerify: true
CacheTTL: 30 seconds
DialTimeout: 10 seconds
WriteTimeout: 10 seconds
ReadTimeout: 30 seconds
Architecture
Connection Flow
Client initiates WebSocket connection
Balancer checks connection counts from all backends
Selects backend with lowest connection count
Establishes connection to chosen backend
Sets up bidirectional relay
Monitors connection health
Load Balancing Algorithm
Maintains cache of connection counts
Updates counts based on CacheTTL
Uses atomic operations for thread safety
Handles backend failures gracefully
Development
Project Structure
traefikwsbalancer/
├── cmd/
│ ├── test-client/
│ │ └── main.go
│ └── test-server/
│ └── main.go
├── traefikwsbalancer.go
├── go.mod
└── go.sum
Testing
The project includes several test types:
Unit tests for core functionality
Integration tests for WebSocket handling
Load tests for connection management
Run tests:
Production Deployment
Best Practices
Security
Enable TLS verification in production
Implement proper origin checking
Set appropriate timeouts
Performance
Adjust CacheTTL based on load
Monitor connection counts
Set appropriate buffer sizes
Monitoring
Log connection events
Track metrics
Monitor backend health
Example Configuration
Copy wsbalancer :
metricPath : "/metric"
pods :
- "http://backend1:8080"
- "http://backend2:8080"
tlsVerify : true
cacheTTL : "30s"
dialTimeout : "10s"
writeTimeout : "10s"
readTimeout : "30s"
Troubleshooting
Common Issues
Connection Failures
Check backend availability
Verify network connectivity
Check TLS configuration
Performance Issues
Monitor connection counts
Adjust cache TTL
Check backend resources
Protocol Errors
Verify WebSocket upgrade headers
Check protocol compatibility
Monitor message sizes
Debugging
Enable detailed logging:
Copy log . SetFlags ( log . Lshortfile | log . Ltime | log . Lmicroseconds )
Contributing
Fork the repository
Create your feature branch
Commit your changes
Push to the branch
Create a Pull Request
License
MIT License
Support
For issues and feature requests, please create an issue in the GitHub repository.
Acknowledgments
gorilla/websocket team for the excellent WebSocket implementation
Traefik team for the plugin system
Contributors who have helped improve this project