2323
2424import urllib3 .exceptions
2525import tuf .exceptions
26- import tuf .settings
2726
2827logger = logging .getLogger (__name__ )
2928
@@ -62,7 +61,25 @@ def fetch(self, url, required_length):
6261
6362
6463
65- class RequestsFetcher (FetcherInterface ):
64+ class FetcherSettings ():
65+ """
66+ <Purpose>
67+ Data class. Defines fetcher-specific settings and their default values
68+ """
69+
70+ # The maximum chunk of data, in bytes, we would download in every round.
71+ CHUNK_SIZE = 400000 #bytes
72+ # Set a timeout value in seconds (float) for non-blocking socket operations.
73+ SOCKET_TIMEOUT = 4 #seconds
74+ # The client's update procedure (contained within a while-loop) can potentially
75+ # hog the CPU. The following setting can be used to force the update sequence
76+ # to suspend execution for a specified amount of time. See
77+ # theupdateframework/tuf/issue#338.
78+ SLEEP_BEFORE_ROUND = None
79+
80+
81+
82+ class RequestsFetcher (FetcherInterface , FetcherSettings ):
6683 """
6784 <Purpose>
6885 A concrete implementation of FetcherInterface based on the Requests
@@ -97,7 +114,7 @@ def fetch(self, url, required_length):
97114 # - connect timeout (max delay before first byte is received)
98115 # - read (gap) timeout (max delay between bytes received)
99116 with session .get (url , stream = True ,
100- timeout = tuf . settings .SOCKET_TIMEOUT ) as response :
117+ timeout = self .SOCKET_TIMEOUT ) as response :
101118
102119 # Check response status.
103120 response .raise_for_status ()
@@ -110,11 +127,11 @@ def fetch(self, url, required_length):
110127 # to download an extremely large file in one shot.
111128 # Before beginning the round, sleep (if set) for a short amount of time
112129 # so that the CPU is not hogged in the while loop.
113- if tuf . settings .SLEEP_BEFORE_ROUND :
114- time .sleep (tuf . settings .SLEEP_BEFORE_ROUND )
130+ if self .SLEEP_BEFORE_ROUND :
131+ time .sleep (self .SLEEP_BEFORE_ROUND )
115132
116133 read_amount = min (
117- tuf . settings .CHUNK_SIZE , required_length - bytes_received )
134+ self .CHUNK_SIZE , required_length - bytes_received )
118135
119136 # NOTE: This may not handle some servers adding a Content-Encoding
120137 # header, which may cause urllib3 to misbehave:
0 commit comments