-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhaproxy_backend_server1.py
65 lines (56 loc) · 2.18 KB
/
haproxy_backend_server1.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python3
""" Simple TCP Server
* respond to simple commands
* can be used to illustrate persistent connections through HAProxy
* each thread handles a connection simultaneous and independent
* do not use Queue because is FIFO which can't be parallelized
"""
import socket
import threading
from threading import Thread
import re
servername = "A"
port = 9999
max_open_connections = 50
active_connections = 0
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("localhost", port))
sock.listen()
def process_connection(conn, addr):
"""Handles an incoming tcp connection.
Close if too much data is read.
Close if the other end closes.
"""
global active_connections
threadId = threading.get_ident()
print(f"Server {servername}, Thread {threadId}: Procesing connection {addr}")
while True: # persistent connection, keep processing
try:
# blocks until new data in socket buffer
data = conn.recv(8)
if len(data) > 6:
print(f"Server {servername}, Thread {threadId}: Received too much. Closing this connection.")
break
command = str(data, encoding="utf-8")
print(f"Server {servername}, Thread {threadId}: Received command: {command}.")
if re.search("status", command):
conn.sendall(bytes(f"Server {servername}, thread {threadId}: Health ok.", "utf-8"))
else:
conn.sendall(bytes(f"Server {servername}, thread {threadId}: Invalid command, try again.", "utf-8"
))
except Exception as e:
print(f"Server {servername}, Thread {threadId}: Error {e}. Closing thread.")
break
conn.close()
active_connections -= 1
while True:
print(f"Server {servername} Waiting for a new connection ...")
conn, addr = sock.accept() # blocks until there is a connection
active_connections += 1
if active_connections > max_open_connections:
print(f"Server {servername}: Number of allowed connections exceeded, closing new connection.")
conn.close()
active_connections -= 1
else:
t = Thread(target=process_connection, args=(conn, addr))
t.start()