MCPcopy
hub / github.com/kubernetes/test-infra / ReadHTTP

Function ReadHTTP

robots/issue-creator/sources/flakyjob-reporter.go:256–282  ·  view source on GitHub ↗

ReadHTTP fetches file contents from a URL with retries.

(url string)

Source from the content-addressed store, hash-verified

254
255// ReadHTTP fetches file contents from a URL with retries.
256func ReadHTTP(url string) ([]byte, error) {
257 var err error
258 retryDelay := time.Duration(2) * time.Second
259 for retryCount := 0; retryCount < 5; retryCount++ {
260 if retryCount > 0 {
261 time.Sleep(retryDelay)
262 retryDelay *= time.Duration(2)
263 }
264
265 resp, err := http.Get(url)
266 if resp != nil && resp.StatusCode >= 500 {
267 // Retry on this type of error.
268 continue
269 }
270 if err != nil {
271 return nil, err
272 }
273 defer resp.Body.Close()
274
275 body, err := io.ReadAll(resp.Body)
276 if err != nil {
277 continue
278 }
279 return body, nil
280 }
281 return nil, fmt.Errorf("ran out of retries reading from '%s'. Last error was %w", url, err)
282}

Callers 2

IssuesMethod · 0.85
IssuesMethod · 0.85

Calls 2

GetMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected