Skip to content

Commit

Permalink
only send lastmodified and etag if present (#479)
Browse files Browse the repository at this point in the history
* only send lastmodified and etag if present

* fix cyclomatic complexity

* remove empty line
  • Loading branch information
arriven authored Apr 2, 2022
1 parent f3f77c8 commit d034065
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/job/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func fetchSingle(path string, lastKnownConfig *RawMultiConfig) (*RawMultiConfig,
return &RawMultiConfig{Body: res, lastModified: "", etag: ""}, nil
}

return fetchURL(configURL, lastKnownConfig)
}

func fetchURL(configURL *url.URL, lastKnownConfig *RawMultiConfig) (*RawMultiConfig, error) {
const requestTimeout = 20 * time.Second

ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
Expand All @@ -102,8 +106,13 @@ func fetchSingle(path string, lastKnownConfig *RawMultiConfig) (*RawMultiConfig,
return nil, err
}

req.Header.Add("If-None-Match", lastKnownConfig.etag)
req.Header.Add("If-Modified-Since", lastKnownConfig.lastModified)
if lastKnownConfig.etag != "" {
req.Header.Add("If-None-Match", lastKnownConfig.etag)
}

if lastKnownConfig.lastModified != "" {
req.Header.Add("If-Modified-Since", lastKnownConfig.lastModified)
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down

0 comments on commit d034065

Please sign in to comment.