-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_balancer.py
67 lines (51 loc) · 1.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from server import Server
from user import User
class LoadBalancer:
def __init__(self, ttask: int, umax: int):
self.TTASK = ttask
self.UMAX = umax
self.servers = list()
self.total = int()
self.total_users = int()
def calculate_user(self) -> int:
"""Count the total of users between all servers."""
total_users = int()
for server in self.servers:
total_users += server.total_users()
self.total_users = total_users
return total_users
def create_a_server(self) -> Server:
""""Create a server instance."""
server = Server(self.UMAX)
return server
def assign_task_to_server(self, users) -> None:
"""Assign a task to a server.
Assign a task to an existent server or create a
new server and assign the task to the new instance.
"""
for i in range(users):
for server in self.servers:
if server.available():
server.connect(User(self.TTASK))
break
else:
server = self.create_a_server()
server.connect(User(self.TTASK))
self.servers.append(server)
def run_tasks(self):
"""Execute tasks in all servers.
Execute all tasks in all servers, reducing the ttask by one,
also sum the count of servers to represent how much was the cost
for each tick run. After execute tasks, if some of the servers are
empty it will be removed from servers attribute.
"""
for server in self.servers:
server.execute_tasks()
self.total += len(self.servers)
self.servers = [s for s in self.servers if not s.is_empty()]
def output(self):
""""Just an output message.
An output representation composed by length of users in each server,
separated by comma.
"""
return ", ".join(str(len(s.users)) for s in self.servers)