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

Get avg_time directly instead of carrying it around.

This commit is contained in:
Markus Birth 2018-02-07 01:21:29 +01:00
parent dbaced1fe8
commit 34ac668d56
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
2 changed files with 6 additions and 6 deletions

View File

@ -57,8 +57,9 @@ class ServerVoteMixin:
"""Return average connection time."""
return self.check_time_sum / self.check_time_count
def master_server_vote_on_time(self, last_duration, avg_duration):
def master_server_vote_on_time(self, last_duration):
"""Change weight of a server based on average connection time."""
avg_duration = self.check_time_avg()
if last_duration < avg_duration - 0.5:
self.master_server_upvote()
elif last_duration > avg_duration + 0.5:

View File

@ -61,16 +61,15 @@ class TclCheckMixin:
reqtime_start = time.perf_counter()
req = self.sess.get(url, params=params, timeout=timeout)
reqtime = time.perf_counter() - reqtime_start
reqtime_avg = self.check_time_avg()
self.check_time_add(reqtime)
last_response = req
if req.status_code == 200:
self.master_server_vote_on_time(reqtime, reqtime_avg)
self.master_server_vote_on_time(reqtime)
req.encoding = "utf-8" # Force encoding as server doesn't give one
self.write_dump(req.text)
return req.text
elif req.status_code not in [500, 502, 503]:
self.do_check_errorhandle(req, reqtime, reqtime_avg)
self.do_check_errorhandle(req, reqtime)
except requests.exceptions.Timeout:
pass
# Something went wrong, try a different server
@ -79,13 +78,13 @@ class TclCheckMixin:
url = self.prep_check_url(https)
raise requests.exceptions.RetryError("Max tries ({}) reached.".format(max_tries), response=last_response)
def do_check_errorhandle(self, req, reqtime, reqtime_avg):
def do_check_errorhandle(self, req, reqtime):
"""Handle non-HTTP 200 results for ``do_check``."""
errcodes = defaultdict(lambda: "HTTP {}.".format(req.status_code))
errcodes[204] = "No update available."
errcodes[404] = "No data for requested CUREF/FV combination."
if req.status_code in [204, 404]:
self.master_server_vote_on_time(reqtime, reqtime_avg)
self.master_server_vote_on_time(reqtime)
elif req.status_code not in [500, 502, 503]:
self.master_server_downvote()
req.raise_for_status()