Skip to content

Commit b115d70

Browse files
committed
Take out connection time from download speed calculation
- Update RequestsFetcher.fetch to return a generator object. - Update FileDownload 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 b1cb6e8 commit b115d70

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

tuf/download.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,33 +204,35 @@ def _download_file(self, url, required_length, STRICT_REQUIRED_LENGTH=True):
204204
# This is the temporary file that we will return to contain the contents of
205205
# the downloaded file.
206206
temp_file = tempfile.TemporaryFile()
207-
start_time = timeit.default_timer()
208207

209208
average_download_speed = 0
210209
number_of_bytes_received = 0
211210

212211
try:
213212

214-
for chunk in self.fetcher.fetch(url, required_length):
213+
chunks = self.fetcher.fetch(url, required_length)
214+
start_time = timeit.default_timer()
215+
for chunk in chunks:
215216

216217
stop_time = timeit.default_timer()
217-
seconds_spent_receiving = stop_time - start_time
218+
temp_file.write(chunk)
219+
218220
# Measure the average download speed.
219221
number_of_bytes_received += len(chunk)
222+
seconds_spent_receiving = stop_time - start_time
220223
average_download_speed = number_of_bytes_received / seconds_spent_receiving
221224

222225
if average_download_speed < tuf.settings.MIN_AVERAGE_DOWNLOAD_SPEED:
223226
logger.debug('The average download speed dropped below the minimum'
224-
' average download speed set in tuf.settings.py.')
227+
' average download speed set in tuf.settings.py. Stopping the'
228+
' download!')
225229
break
226230

227231
else:
228232
logger.debug('The average download speed has not dipped below the'
229233
' minimum average download speed set in tuf.settings.py.')
230234

231235

232-
temp_file.write(chunk)
233-
234236
# Does the total number of downloaded bytes match the required length?
235237
self._check_downloaded_length(number_of_bytes_received, required_length,
236238
STRICT_REQUIRED_LENGTH=STRICT_REQUIRED_LENGTH,

tuf/fetcher.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,11 @@ def fetch(self, url, required_length):
113113
# Always set the timeout. This timeout value is interpreted by requests as:
114114
# - connect timeout (max delay before first byte is received)
115115
# - read (gap) timeout (max delay between bytes received)
116-
with session.get(url, stream=True,
117-
timeout=self.SOCKET_TIMEOUT) as response:
118-
119-
# Check response status.
120-
response.raise_for_status()
116+
response = session.get(url, stream=True, timeout=self.SOCKET_TIMEOUT)
117+
# Check response status.
118+
response.raise_for_status()
121119

120+
def chunks():
122121
try:
123122
bytes_received = 0
124123
while True:
@@ -139,11 +138,6 @@ def fetch(self, url, required_length):
139138
data = response.raw.read(read_amount)
140139
bytes_received += len(data)
141140

142-
yield data
143-
144-
if bytes_received == required_length:
145-
break
146-
147141
# We might have no more data to read. Check number of bytes downloaded.
148142
if not data:
149143
logger.debug('Downloaded ' + repr(bytes_received) + '/' +
@@ -152,9 +146,15 @@ def fetch(self, url, required_length):
152146
# Finally, we signal that the download is complete.
153147
break
154148

149+
yield data
150+
151+
if bytes_received == required_length:
152+
break
153+
155154
except urllib3.exceptions.ReadTimeoutError as e:
156155
raise tuf.exceptions.SlowRetrievalError(str(e))
157156

157+
return chunks()
158158

159159

160160

0 commit comments

Comments
 (0)