ReadHTTP fetches file contents from a URL with retries.
(url string)
| 254 | |
| 255 | // ReadHTTP fetches file contents from a URL with retries. |
| 256 | func 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 | } |