Skip to content

Commit 77900af

Browse files
committed
Take out connection time from download speed calculation
- Update RequestsFetcher.fetch to return a generator object. - Update _download_file to skip connection time when calculating average download speed. - Write chunk to temp file before exiting the fetcher loop on error. Signed-off-by: Teodora Sechkova <[email protected]>
1 parent 454614a commit 77900af

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

tuf/download.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,33 +190,33 @@ def _download_file(url, required_length, fetcher, STRICT_REQUIRED_LENGTH=True):
190190
# This is the temporary file that we will return to contain the contents of
191191
# the downloaded file.
192192
temp_file = tempfile.TemporaryFile()
193-
start_time = timeit.default_timer()
194193

195194
average_download_speed = 0
196195
number_of_bytes_received = 0
197196

198197
try:
199-
200-
for chunk in fetcher.fetch(url, required_length):
198+
chunks = fetcher.fetch(url, required_length)
199+
start_time = timeit.default_timer()
200+
for chunk in chunks:
201201

202202
stop_time = timeit.default_timer()
203-
seconds_spent_receiving = stop_time - start_time
203+
temp_file.write(chunk)
204+
204205
# Measure the average download speed.
205206
number_of_bytes_received += len(chunk)
207+
seconds_spent_receiving = stop_time - start_time
206208
average_download_speed = number_of_bytes_received / seconds_spent_receiving
207209

208210
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
209211
logger.debug('The average download speed dropped below the minimum'
210-
' average download speed set in tuf.settings.py.')
212+
' average download speed set in tuf.settings.py. Stopping the'
213+
' download!')
211214
break
212215

213216
else:
214217
logger.debug('The average download speed has not dipped below the'
215218
' minimum average download speed set in tuf.settings.py.')
216219

217-
218-
temp_file.write(chunk)
219-
220220
# Does the total number of downloaded bytes match the required length?
221221
_check_downloaded_length(number_of_bytes_received, required_length,
222222
STRICT_REQUIRED_LENGTH=STRICT_REQUIRED_LENGTH,

tuf/fetcher.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ def fetch(self, url, required_length):
9696
# Always set the timeout. This timeout value is interpreted by requests as:
9797
# - connect timeout (max delay before first byte is received)
9898
# - read (gap) timeout (max delay between bytes received)
99-
with session.get(url, stream=True,
100-
timeout=tuf.settings.SOCKET_TIMEOUT) as response:
101-
102-
# Check response status.
103-
response.raise_for_status()
99+
response = session.get(url, stream=True,
100+
timeout=tuf.settings.SOCKET_TIMEOUT)
101+
# Check response status.
102+
response.raise_for_status()
104103

104+
def chunks():
105105
try:
106106
bytes_received = 0
107107
while True:
@@ -122,11 +122,6 @@ def fetch(self, url, required_length):
122122
data = response.raw.read(read_amount)
123123
bytes_received += len(data)
124124

125-
yield data
126-
127-
if bytes_received == required_length:
128-
break
129-
130125
# We might have no more data to read. Check number of bytes downloaded.
131126
if not data:
132127
logger.debug('Downloaded ' + repr(bytes_received) + '/' +
@@ -135,9 +130,18 @@ def fetch(self, url, required_length):
135130
# Finally, we signal that the download is complete.
136131
break
137132

133+
yield data
134+
135+
if bytes_received == required_length:
136+
break
137+
138138
except urllib3.exceptions.ReadTimeoutError as e:
139139
raise tuf.exceptions.SlowRetrievalError(str(e))
140140

141+
finally:
142+
response.close()
143+
144+
return chunks()
141145

142146

143147

0 commit comments

Comments
 (0)