Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Retries into the Helm http getter, for 502/503 status codes. #12976

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 57 additions & 9 deletions pkg/getter/httpgetter.go
Expand Up @@ -18,10 +18,12 @@ package getter
import (
"bytes"
"crypto/tls"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -83,18 +85,64 @@ func (g *HTTPGetter) get(href string) (*bytes.Buffer, error) {
return nil, err
}

resp, err := client.Do(req)
if err != nil {
return nil, err
// Define the maximum number of retries
maxRetries := 20

// Define retryable status codes
retryableCodes := map[int]bool{
http.StatusBadGateway: true, // 502
http.StatusServiceUnavailable: true, // 503
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("failed to fetch %s : %s", href, resp.Status)

for attempt := 0; attempt < maxRetries; attempt++ {
resp, err := client.Do(req)

if err != nil {
if attempt < (maxRetries - 1) {
fmt.Printf("Error: %v\n", err)
// Sleep for a short duration before retrying
time.Sleep(1 * time.Second)
continue
}

return nil, err
}

respStatus := resp.Status
defer resp.Body.Close()

// Check the status code
if _, ok := retryableCodes[resp.StatusCode]; ok {
if attempt < (maxRetries - 1) {
fmt.Printf("Received retryable status code: %v\n", resp.StatusCode)
// Close the response body
resp.Body.Close()
// Sleep for a short duration before retrying
time.Sleep(1 * time.Second)
continue
}
return nil, errors.Errorf("failed to fetch %s : %s", href, respStatus)
}

if resp.StatusCode != http.StatusOK {
if attempt < (maxRetries - 1) {
fmt.Printf("Received non-OK status code: %v\n", resp.StatusCode)
// Close the response body
resp.Body.Close()
// Sleep for a short duration before retrying
time.Sleep(1 * time.Second)
continue
}
return nil, errors.Errorf("failed to fetch %s : %s", href, respStatus)
}

// Process the response
buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, resp.Body)
return buf, err
}

buf := bytes.NewBuffer(nil)
_, err = io.Copy(buf, resp.Body)
return buf, err
return nil, fmt.Errorf("maximum retries exceeded")
}

// NewHTTPGetter constructs a valid http/https client as a Getter
Expand Down