Skip to content

Commit

Permalink
Add retry to get release request (#3562)
Browse files Browse the repository at this point in the history
  • Loading branch information
Feroze Mohideen authored Sep 13, 2023
1 parent 3c97747 commit c584f66
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
53 changes: 46 additions & 7 deletions api/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,36 @@ func NewClientWithConfig(ctx context.Context, input NewClientInput) (Client, err
// ErrNoAuthCredential returns an error when no auth credentials have been provided such as cookies or tokens
var ErrNoAuthCredential = errors.New("unable to create an API session with cookie nor token")

func (c *Client) getRequest(relPath string, data interface{}, response interface{}) error {
// getRequestConfig defines configuration for a GET request
type getRequestConfig struct {
retryCount uint
}

// withRetryCount is a convenience function for setting the retry count
func withRetryCount(retryCount uint) func(*getRequestConfig) {
return func(o *getRequestConfig) {
o.retryCount = retryCount
}
}

// getRequest is responsible for making a GET request to the API
func (c *Client) getRequest(relPath string, data interface{}, response interface{}, opts ...func(*getRequestConfig)) error {
config := &getRequestConfig{
retryCount: 1,
}

for _, opt := range opts {
opt(config)
}

var httpErr *types.ExternalError
var err error

vals := make(map[string][]string)
err := schema.NewEncoder().Encode(data, vals)
err = schema.NewEncoder().Encode(data, vals)
if err != nil {
return err
}

urlVals := url.Values(vals)
encodedURLVals := urlVals.Encode()
Expand All @@ -106,15 +133,27 @@ func (c *Client) getRequest(relPath string, data interface{}, response interface
return err
}

if httpErr, err := c.sendRequest(req, response, true); httpErr != nil || err != nil {
if httpErr != nil {
return fmt.Errorf("%v", httpErr.Error)
for i := 0; i < int(config.retryCount); i++ {
httpErr, err = c.sendRequest(req, response, true)

if httpErr == nil && err == nil {
return nil
}

return err
if i != int(config.retryCount)-1 {
if httpErr != nil {
fmt.Fprintf(os.Stderr, "Error: %s (status code %d), retrying request...\n", httpErr.Error, httpErr.Code)
} else {
fmt.Fprintf(os.Stderr, "Error: %v, retrying request...\n", err)
}
}
}

return nil
if httpErr != nil {
return fmt.Errorf("%v", httpErr.Error)
}

return err
}

type postRequestOpts struct {
Expand Down
1 change: 1 addition & 0 deletions api/client/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func (c *Client) GetRelease(
),
nil,
resp,
withRetryCount(3),
)

return resp, err
Expand Down

0 comments on commit c584f66

Please sign in to comment.