-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balance_logic.py
36 lines (31 loc) · 991 Bytes
/
load_balance_logic.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
import json
import os
from topo import N_SERVERS
import requests
import time
server = ['10.0.0.'+str(i) for i in range(1,N_SERVERS+1)]
class LB:
def __init__(self):
self.agg_data = {}
self.last_read = 0
def read(self):
if time.time()-self.last_read>0.3:
for s in server:
t = time.time()
response = requests.get('http://'+s+':5000/usage_stats')
resp_time = time.time()-t
data = response.json()
data['response_time'] = resp_time
self.agg_data[s] = data
self.last_read = time.time()
return self.agg_data
def lb_next(self,metric='cpu'):
data = self.read()
minServer = None
minMetric = float('inf')
for k,v in data.items():
if v[metric]<minMetric:
minServer = k
minMetric = v[metric]
print(k,v[metric])
return minServer