This project implements a high-performance HTTP/2 server with advanced load balancing capabilities using Node.js and TypeScript. It's designed to handle high-traffic scenarios, providing features like session management, rate limiting, and caching to ensure optimal performance and security. Features
HTTP/2 support with secure HTTPS connections Advanced load balancing with multiple algorithms:
- Round-robin
- Least connections
- Weighted round-robin
- IP hash
- Least response time
- Consistent hashing
Session management Rate limiting In-memory caching Custom request handling Health checks for backend servers Metrics collection and reporting
Project Structure The project consists of several TypeScript files, each responsible for a specific functionality:
http2Server.ts: Main server implementation loadBalancer.ts: Load balancing logic sessionManager.ts: Session management rateLimiter.ts: Rate limiting implementation caching.ts: In-memory caching types.ts: TypeScript interfaces and types example-usage.ts: Example usage of the server
Prerequisites
Node.js (v14.x or later) TypeScript (v4.x or later) OpenSSL (for generating SSL certificates)
Installation
Clone the repository: Copygit clone https://github.com/yourusername/http2-load-balancer.git cd http2-load-balancer
Install dependencies: Copynpm install
Generate SSL certificates:
Copyopenssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost'
-keyout server.key -out server.crt
Configuration The server can be configured using various configuration objects:
ServerConfig: Main server configuration LoadBalancerConfig: Load balancer settings SessionConfig: Session management configuration RateLimitConfig: Rate limiting settings CacheConfig: Caching configuration
Refer to the example-usage.ts file for configuration examples. Usage
Set up your configuration in a new TypeScript file (e.g., app.ts). Import the necessary components and create an instance of HTTP2Server. Add backend servers using the addBackend method. Implement your custom request handler using setRequestHandler. Start the server using the start method.
Example: typescriptCopyimport { HTTP2Server } from './http2Server'; // ... import other necessary types and configurations
const server = new HTTP2Server(serverConfig, loadBalancerConfig, sessionConfig, rateLimitConfig, cacheConfig);
// Add backends server.addBackend({ url: 'https://api1.example.com', weight: 1, maxConnections: 100 }); server.addBackend({ url: 'https://api2.example.com', weight: 2, maxConnections: 200 });
// Set custom request handler server.setRequestHandler(async (stream, headers, payload) => { // Implement your request handling logic here });
// Start the server server.start(); Run your application: Copyts-node app.ts API Endpoints The server doesn't have predefined endpoints. You need to implement your own endpoints in the custom request handler. The example-usage.ts file demonstrates how to create endpoints for:
/api/users: A GET endpoint that returns a list of users (demonstrates caching) /api/login: A POST endpoint for user authentication (demonstrates session management) /api/protected: A GET endpoint that requires authentication (demonstrates using session data)
Advanced Features Load Balancing The load balancer supports multiple algorithms. You can choose the algorithm by setting the algorithm property in the LoadBalancerConfig:
'round-robin': Distributes requests evenly across all backends 'least-connections': Sends requests to the backend with the fewest active connections 'weighted-round-robin': Like round-robin, but backends can handle more requests based on their weight 'ip-hash': Consistently maps IP addresses to specific backends 'least-response-time': Sends requests to the backend with the lowest average response time 'consistent-hashing': Provides a consistent mapping of requests to backends, useful for caching scenarios
Session Management The SessionManager class handles creating, updating, and cleaning up sessions. Sessions are stored in-memory and can be accessed in your request handler. Rate Limiting The RateLimiter class provides basic IP-based rate limiting. You can configure the time window and maximum number of requests allowed per IP address. Caching The CacheManager class provides in-memory caching of responses. You can configure which responses should be cached in your custom request handler. Monitoring and Metrics The server provides basic metrics through the getMetrics method. You can periodically call this method to log or expose metrics about your server's performance. Security Considerations
Always use HTTPS in production environments. Keep your SSL certificates secure and up-to-date. Implement proper authentication and authorization in your application logic. Regularly update dependencies to patch any security vulnerabilities. Consider implementing additional security headers (e.g., HSTS, CSP) in your responses.
Future Enhancements
Implement WebSocket support Add support for external caching systems (e.g., Redis) Implement circuit breaker pattern for better fault tolerance Add support for dynamic backend configuration updates Implement more comprehensive logging and tracing
Contributing Contributions are welcome! Please feel free to submit a Pull Request. License This project is licensed under the MIT License.