Browse Source

Changes to determining can_be_slowed (#1891)

* Changes to determining can_be_slowed

* Add return type for get_stable_speed and make can_be_slowed local
pull/1920/head
puzzledsab 4 years ago
committed by GitHub
parent
commit
c90a93661b
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 14
      sabnzbd/bpsmeter.py
  2. 47
      sabnzbd/downloader.py

14
sabnzbd/bpsmeter.py

@ -425,14 +425,22 @@ class BPSMeter:
# We record every second, but display at the user's refresh-rate
return self.bps_list[::refresh_rate]
def get_stable_speed(self, timespan=10):
def get_stable_speed(self, timespan: int = 10) -> Optional[int]:
"""See if there is a stable speed the last <timespan> seconds
None: indicates it can't determine yet
False: the speed was not stable during <timespan>
0: the speed was not stable during <timespan>
Positive float: the speed was stable
"""
if len(self.bps_list) < timespan:
return None
# Check if speed fell by more than 15%
try:
if self.bps_list[-1] / self.bps_list[-timespan] < 0.85:
return 0
except:
pass
# Calculate the variance in the speed
avg = sum(self.bps_list[-timespan:]) / timespan
vari = 0
@ -445,7 +453,7 @@ class BPSMeter:
if (vari / (self.bps / KIBI)) < 0.05:
return avg
else:
return False
return 0
except:
# Probably one of the values was 0
pass

47
sabnzbd/downloader.py

@ -242,8 +242,6 @@ class Downloader(Thread):
"paused",
"bandwidth_limit",
"bandwidth_perc",
"can_be_slowed",
"can_be_slowed_timer",
"sleep_time",
"paused_for_postproc",
"shutdown",
@ -270,8 +268,6 @@ class Downloader(Thread):
self.speed_set()
# Used to see if we can add a slowdown to the Downloader-loop
self.can_be_slowed: Optional[bool] = None
self.can_be_slowed_timer: int = 0
self.sleep_time: float = 0.0
self.sleep_time_set()
cfg.downloader_sleep_time.callback(self.sleep_time_set)
@ -544,6 +540,11 @@ class Downloader(Thread):
BPSMeter.update()
next_bpsmeter_update = 0
# can_be_slowed variables
can_be_slowed: Optional[float] = None
can_be_slowed_timer: float = 0.0
next_stable_speed_check: float = 0.0
# Check server expiration dates
check_server_expiration()
@ -678,21 +679,29 @@ class Downloader(Thread):
read, _, _ = select.select(readkeys, (), (), 1.0)
# Add a sleep if there are too few results compared to the number of active connections
if self.can_be_slowed and len(read) < 1 + len(readkeys) / 10:
time.sleep(self.sleep_time)
# Need to initialize the check during first 20 seconds
if self.can_be_slowed is None or self.can_be_slowed_timer:
# Wait for stable speed to start testing
if not self.can_be_slowed_timer and BPSMeter.get_stable_speed(timespan=10):
self.can_be_slowed_timer = time.time()
# Check 10 seconds after enabling slowdown
if self.can_be_slowed_timer and time.time() > self.can_be_slowed_timer + 10:
# Now let's check if it was stable in the last 10 seconds
self.can_be_slowed = BPSMeter.get_stable_speed(timespan=10)
self.can_be_slowed_timer = 0
logging.debug("Downloader-slowdown: %r", self.can_be_slowed)
if self.sleep_time:
if can_be_slowed and len(read) < 1 + len(readkeys) / 10:
time.sleep(self.sleep_time)
# Initialize by waiting for stable speed and then enable sleep
if can_be_slowed is None or can_be_slowed_timer:
# Wait for stable speed to start testing
if not can_be_slowed_timer and now > next_stable_speed_check:
if BPSMeter.get_stable_speed(timespan=10):
can_be_slowed_timer = now + 8
can_be_slowed = 1
else:
next_stable_speed_check = now + _BPSMETER_UPDATE_DELAY
# Check 10 seconds after enabling slowdown
if can_be_slowed_timer and now > can_be_slowed_timer:
# Now let's check if it was stable in the last 10 seconds
can_be_slowed = BPSMeter.get_stable_speed(timespan=10)
can_be_slowed_timer = 0
if not can_be_slowed:
self.sleep_time = 0
logging.debug("Downloader-slowdown: %r", can_be_slowed)
else:
read = []

Loading…
Cancel
Save