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 # We record every second, but display at the user's refresh-rate
return self.bps_list[::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 """See if there is a stable speed the last <timespan> seconds
None: indicates it can't determine yet 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: if len(self.bps_list) < timespan:
return None 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 # Calculate the variance in the speed
avg = sum(self.bps_list[-timespan:]) / timespan avg = sum(self.bps_list[-timespan:]) / timespan
vari = 0 vari = 0
@ -445,7 +453,7 @@ class BPSMeter:
if (vari / (self.bps / KIBI)) < 0.05: if (vari / (self.bps / KIBI)) < 0.05:
return avg return avg
else: else:
return False return 0
except: except:
# Probably one of the values was 0 # Probably one of the values was 0
pass pass

47
sabnzbd/downloader.py

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

Loading…
Cancel
Save