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:
parent
dbaced1fe8
commit
34ac668d56
@ -57,8 +57,9 @@ class ServerVoteMixin:
|
|||||||
"""Return average connection time."""
|
"""Return average connection time."""
|
||||||
return self.check_time_sum / self.check_time_count
|
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."""
|
"""Change weight of a server based on average connection time."""
|
||||||
|
avg_duration = self.check_time_avg()
|
||||||
if last_duration < avg_duration - 0.5:
|
if last_duration < avg_duration - 0.5:
|
||||||
self.master_server_upvote()
|
self.master_server_upvote()
|
||||||
elif last_duration > avg_duration + 0.5:
|
elif last_duration > avg_duration + 0.5:
|
||||||
|
@ -61,16 +61,15 @@ class TclCheckMixin:
|
|||||||
reqtime_start = time.perf_counter()
|
reqtime_start = time.perf_counter()
|
||||||
req = self.sess.get(url, params=params, timeout=timeout)
|
req = self.sess.get(url, params=params, timeout=timeout)
|
||||||
reqtime = time.perf_counter() - reqtime_start
|
reqtime = time.perf_counter() - reqtime_start
|
||||||
reqtime_avg = self.check_time_avg()
|
|
||||||
self.check_time_add(reqtime)
|
self.check_time_add(reqtime)
|
||||||
last_response = req
|
last_response = req
|
||||||
if req.status_code == 200:
|
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
|
req.encoding = "utf-8" # Force encoding as server doesn't give one
|
||||||
self.write_dump(req.text)
|
self.write_dump(req.text)
|
||||||
return req.text
|
return req.text
|
||||||
elif req.status_code not in [500, 502, 503]:
|
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:
|
except requests.exceptions.Timeout:
|
||||||
pass
|
pass
|
||||||
# Something went wrong, try a different server
|
# Something went wrong, try a different server
|
||||||
@ -79,13 +78,13 @@ class TclCheckMixin:
|
|||||||
url = self.prep_check_url(https)
|
url = self.prep_check_url(https)
|
||||||
raise requests.exceptions.RetryError("Max tries ({}) reached.".format(max_tries), response=last_response)
|
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``."""
|
"""Handle non-HTTP 200 results for ``do_check``."""
|
||||||
errcodes = defaultdict(lambda: "HTTP {}.".format(req.status_code))
|
errcodes = defaultdict(lambda: "HTTP {}.".format(req.status_code))
|
||||||
errcodes[204] = "No update available."
|
errcodes[204] = "No update available."
|
||||||
errcodes[404] = "No data for requested CUREF/FV combination."
|
errcodes[404] = "No data for requested CUREF/FV combination."
|
||||||
if req.status_code in [204, 404]:
|
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]:
|
elif req.status_code not in [500, 502, 503]:
|
||||||
self.master_server_downvote()
|
self.master_server_downvote()
|
||||||
req.raise_for_status()
|
req.raise_for_status()
|
||||||
|
Loading…
Reference in New Issue
Block a user