Skip to content

Commit

Permalink
Merge pull request #554 from dgonyeo/v0.25.0-1branch
Browse files Browse the repository at this point in the history
v0.25.1
  • Loading branch information
Derek Gonyeo authored May 23, 2018
2 parents 52d1d96 + a0da6a8 commit fed0e72
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
6 changes: 6 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
22-May-2018 IGNITION v0.25.1

Bug Fixes:

- Fix an issue in timeout logic causing http(s) requests to sometimes fail

17-May-2018 IGNITION v0.25.0

Features:
Expand Down
19 changes: 10 additions & 9 deletions internal/resource/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,14 @@ func (f *Fetcher) newHttpClient() {
}
}

// getReaderWithHeader performs an HTTP GET on the provided URL with the provided request header
// and returns the response body Reader, HTTP status code, and error (if any). By
// getReaderWithHeader performs an HTTP GET on the provided URL with the
// provided request header and returns the response body Reader, HTTP status
// code, a cancel function for the result's context, and error (if any). By
// default, User-Agent is added to the header but this can be overridden.
func (c HttpClient) getReaderWithHeader(ctx context.Context, url string, header http.Header) (io.ReadCloser, int, error) {
func (c HttpClient) getReaderWithHeader(url string, header http.Header) (io.ReadCloser, int, context.CancelFunc, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, 0, err
return nil, 0, nil, err
}

req.Header.Set("User-Agent", "Ignition/"+version.Raw)
Expand All @@ -212,10 +213,10 @@ func (c HttpClient) getReaderWithHeader(ctx context.Context, url string, header
}
}

ctx, cancelFn := context.WithCancel(context.Background())
if c.timeout != 0 {
ctxTo, cancel := context.WithTimeout(ctx, c.timeout)
ctx = ctxTo
defer cancel()
cancelFn()
ctx, cancelFn = context.WithTimeout(context.Background(), c.timeout)
}

duration := initialBackoff
Expand All @@ -226,7 +227,7 @@ func (c HttpClient) getReaderWithHeader(ctx context.Context, url string, header
if err == nil {
c.logger.Info("GET result: %s", http.StatusText(resp.StatusCode))
if resp.StatusCode < 500 {
return resp.Body, resp.StatusCode, nil
return resp.Body, resp.StatusCode, cancelFn, nil
}
resp.Body.Close()
} else {
Expand All @@ -242,7 +243,7 @@ func (c HttpClient) getReaderWithHeader(ctx context.Context, url string, header
select {
case <-time.After(duration):
case <-ctx.Done():
return nil, 0, ErrTimeout
return nil, 0, cancelFn, ErrTimeout
}
}
}
8 changes: 6 additions & 2 deletions internal/resource/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,15 @@ func (f *Fetcher) FetchFromTFTP(u url.URL, dest *os.File, opts FetchOptions) err
// FetchFromHTTP fetches a resource from u via HTTP(S) into dest, returning an
// error if one is encountered.
func (f *Fetcher) FetchFromHTTP(u url.URL, dest *os.File, opts FetchOptions) error {
ctx := context.Background()
if f.client == nil {
f.newHttpClient()
}
dataReader, status, err := f.client.getReaderWithHeader(ctx, u.String(), opts.Headers)
dataReader, status, ctxCancel, err := f.client.getReaderWithHeader(u.String(), opts.Headers)
if ctxCancel != nil {
// whatever context getReaderWithHeader created for the request should
// be cancelled once we're done reading the response
defer ctxCancel()
}
if err != nil {
return err
}
Expand Down

0 comments on commit fed0e72

Please sign in to comment.