Skip to content

Commit 2e05c61

Browse files
Added back timeout to the fetcher DownloadFile method to avoid a breaking change. (#685)
* Added back timeout to the fetcher DownloadFile method to avoid a breaking change. The argument is documented as not used/deprecated. Signed-off-by: Fredrik Skogman <kommendorkapten@github.com> * Fixed a spelling error Signed-off-by: Fredrik Skogman <kommendorkapten@github.com> --------- Signed-off-by: Fredrik Skogman <kommendorkapten@github.com>
1 parent e9e0d48 commit 2e05c61

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

internal/testutils/simulator/repository_simulator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ func partition(s string, delimiter string) (string, string) {
303303
return version, role
304304
}
305305

306-
func (rs *RepositorySimulator) DownloadFile(urlPath string, maxLength int64) ([]byte, error) {
306+
func (rs *RepositorySimulator) DownloadFile(urlPath string, maxLength int64, _ time.Duration) ([]byte, error) {
307307
data, err := rs.fetch(urlPath)
308308
if err != nil {
309309
return data, err

metadata/fetcher/fetcher.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,12 @@ type httpClient interface {
3737

3838
// Fetcher interface
3939
type Fetcher interface {
40-
DownloadFile(urlPath string, maxLength int64) ([]byte, error)
40+
// DownloadFile downloads a file from the provided URL, reading
41+
// up to maxLength of bytes before it aborts.
42+
// The timeout argument is deprecated and not used. To configure
43+
// the timeout (or retries), modify the fetcher instead. For the
44+
// DefaultFetcher the underlying HTTP client can be substituted.
45+
DownloadFile(urlPath string, maxLength int64, _ time.Duration) ([]byte, error)
4146
}
4247

4348
// DefaultFetcher implements Fetcher
@@ -55,7 +60,7 @@ func (d *DefaultFetcher) SetHTTPUserAgent(httpUserAgent string) {
5560

5661
// DownloadFile downloads a file from urlPath, errors out if it failed,
5762
// its length is larger than maxLength or the timeout is reached.
58-
func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64) ([]byte, error) {
63+
func (d *DefaultFetcher) DownloadFile(urlPath string, maxLength int64, _ time.Duration) ([]byte, error) {
5964
req, err := http.NewRequest("GET", urlPath, nil)
6065
if err != nil {
6166
return nil, err

metadata/fetcher/fetcher_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func TestDownLoadFile(t *testing.T) {
8585
// run the function under test
8686
fetcher := NewDefaultFetcher()
8787
fetcher.SetHTTPUserAgent("Metadata_Unit_Test/1.0")
88-
data, err := fetcher.DownloadFile(tt.url, tt.maxLength)
88+
data, err := fetcher.DownloadFile(tt.url, tt.maxLength, 0)
8989
// special case if we expect no error
9090
if tt.wantErr == nil {
9191
assert.NoErrorf(t, err, "expected no error but got %v", err)
@@ -156,7 +156,7 @@ func TestDownloadFile_Retry(t *testing.T) {
156156
backoff.WithMaxTries(3),
157157
},
158158
}
159-
data, err := fetcher.DownloadFile("https://jku.github.io/tuf-demo/metadata/1.root.json", 512000)
159+
data, err := fetcher.DownloadFile("https://jku.github.io/tuf-demo/metadata/1.root.json", 512000, 0)
160160
if tt.shouldSucceed {
161161
assert.Equal(t, sampleRootData, data)
162162
assert.NoError(t, err)

metadata/updater/updater.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func (update *Updater) DownloadTarget(targetFile *metadata.TargetFiles, filePath
245245
}
246246
}
247247
fullURL := fmt.Sprintf("%s%s", targetBaseURL, targetRemotePath)
248-
data, err := update.cfg.Fetcher.DownloadFile(fullURL, targetFile.Length)
248+
data, err := update.cfg.Fetcher.DownloadFile(fullURL, targetFile.Length, 0)
249249
if err != nil {
250250
return "", nil, err
251251
}
@@ -648,7 +648,7 @@ func (update *Updater) downloadMetadata(roleName string, length int64, version s
648648
} else {
649649
urlPath = fmt.Sprintf("%s%s.%s.json", urlPath, version, url.PathEscape(roleName))
650650
}
651-
return update.cfg.Fetcher.DownloadFile(urlPath, length)
651+
return update.cfg.Fetcher.DownloadFile(urlPath, length, 0)
652652
}
653653

654654
// generateTargetFilePath generates path from TargetFiles

0 commit comments

Comments
 (0)