-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balancer.py
50 lines (43 loc) · 1.58 KB
/
load_balancer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import socket
import threading
import json
import random
from utils import setup_logger, verify_auth_token
from config import HOST, LOAD_BALANCER_PORT, NUM_NODES, PORT_RANGE_START, SOCKET_TIMEOUT
logger = setup_logger(__name__)
class LoadBalancer:
def __init__(self):
self.nodes = [PORT_RANGE_START + i for i in range(NUM_NODES)]
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.bind((HOST, LOAD_BALANCER_PORT))
self.sock.listen(5)
def start(self):
logger.info(f"Load balancer starting on port {LOAD_BALANCER_PORT}")
while True:
client, addr = self.sock.accept()
threading.Thread(target=self.handle_client, args=(client,)).start()
def handle_client(self, client):
try:
data = client.recv(1024).decode('utf-8')
command = json.loads(data)
if not verify_auth_token(command.get('token')):
response = {"error": "Invalid or missing auth token"}
else:
node = self.choose_node()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as node_sock:
node_sock.settimeout(SOCKET_TIMEOUT)
node_sock.connect((HOST, node))
node_sock.sendall(data.encode('utf-8'))
response = node_sock.recv(1024).decode('utf-8')
client.sendall(response.encode('utf-8'))
except Exception as e:
logger.error(f"Error in load balancer: {str(e)}")
finally:
client.close()
def choose_node(self):
return random.choice(self.nodes)
def start_load_balancer():
load_balancer = LoadBalancer()
load_balancer.start()
if __name__ == "__main__":
start_load_balancer()