-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.py
More file actions
25 lines (21 loc) · 1003 Bytes
/
utility.py
File metadata and controls
25 lines (21 loc) · 1003 Bytes
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
import json
from config import Config
def load_config(filename):
with open(filename, 'r') as file:
data = json.load(file)
return Config(data['healthCheckInterval'], data['servers'], data['listenPort'])
def next_server_least_active(servers):
least_active_connections = -1
least_active_server = servers[0]
for server in servers:
with server.Mutex:
if (server.ActiveConnections < least_active_connections or least_active_connections == -1) and server.Healthy:
least_active_connections = server.ActiveConnections
least_active_server = server
return least_active_server
# Alternative code:
# issue: takes 2 for loops (cost) and doesn't take lock
# healthy_servers = [server for server in servers if server.Healthy]
# if not healthy_servers:
# raise Exception("No healthy servers available")
# return min(healthy_servers, key=lambda x: x.ActiveConnections)