1
0
mirror of https://github.com/mbirth/tcl_ota_check.git synced 2024-11-09 22:06:47 +00:00

Added weighting of servers to preferrably choose faster ones.

This commit is contained in:
Markus Birth 2017-10-06 01:19:56 +02:00
parent b830013711
commit f4319fcbbf
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A

View File

@ -68,6 +68,7 @@ class FotaCheck:
"g2master-ap-north.tclclouds.com",
"g2master-sa-east.tclclouds.com",
]
self.master_servers_weights = [3] * len(self.master_servers)
self.reset_session()
def reset_session(self):
@ -88,6 +89,16 @@ class FotaCheck:
def get_master_server(self):
return random.choice(self.master_servers)
def master_server_downvote(self):
idx = self.master_servers.index(self.g2master)
if self.master_servers_weights[idx] > 1:
self.master_servers_weights[idx] -= 1
def master_server_upvote(self):
idx = self.master_servers.index(self.g2master)
if self.master_servers_weights[idx] < 10:
self.master_servers_weights[idx] += 1
def do_check(self, https=True, timeout=10, max_tries=5):
protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php"
@ -109,17 +120,22 @@ class FotaCheck:
req = self.sess.get(url, params=params, timeout=timeout)
last_response = req
if req.status_code == 200:
self.master_server_upvote()
return req.text
elif req.status_code == 204:
self.master_server_upvote()
raise requests.exceptions.HTTPError("No update available.", response=req)
elif req.status_code == 404:
self.master_server_upvote()
raise requests.exceptions.HTTPError("No data for requested CUREF/FV combination.", response=req)
elif req.status_code not in [500, 503]:
self.master_server_downvote()
req.raise_for_status()
raise requests.exceptions.HTTPError("HTTP {}.".format(req.status_code), response=req)
except requests.exceptions.Timeout:
pass
# Something went wrong, try a different server
self.master_server_downvote()
self.g2master = self.get_master_server()
protocol = "https://" if https else "http://"
url = protocol + self.g2master + "/check.php"