func()

in pkg/platform/api/api.go [102:131]


func (c *apiClient) do(req *http.Request) (*http.Response, error) {
	var response *http.Response
	const maxAttempts = 5
	attempts := 0
	// Retry up to 5 times in case the Update API is busy; Waiting 10 seconds between each attempt.
	for ; attempts < maxAttempts; attempts++ {
		var err error
		response, err = c.httpClient.Do(req)
		if err != nil {
			return nil, errors.Wrapf(err, "update API request error")
		}
		if response.StatusCode >= 200 && response.StatusCode < 300 {
			// Response OK
			break
		} else if response.StatusCode == 423 {
			if attempts < maxAttempts-1 {
				c.log.Info("API server busy, retrying in 10 seconds ...")
				// Retry after ten seconds if we get a 423 Locked response (update API busy)
				time.Sleep(10 * time.Second)
				continue
			}
		}
		// API response was a non-transient error, bail out.
		return response, errors.Errorf("bad http response, status code: %d", response.StatusCode)
	}
	if attempts == 5 {
		return nil, errors.New("update API unavailable: retries exhausted")
	}
	return response, nil
}