Browse Source

Add retry to get release request (#3562)

Feroze Mohideen 2 years ago
parent
commit
c584f669bc
2 changed files with 47 additions and 7 deletions
  1. 46 7
      api/client/api.go
  2. 1 0
      api/client/k8s.go

+ 46 - 7
api/client/api.go

@@ -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
 // 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")
 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)
 	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)
 	urlVals := url.Values(vals)
 	encodedURLVals := urlVals.Encode()
 	encodedURLVals := urlVals.Encode()
@@ -106,15 +133,27 @@ func (c *Client) getRequest(relPath string, data interface{}, response interface
 		return err
 		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 {
 type postRequestOpts struct {

+ 1 - 0
api/client/k8s.go

@@ -182,6 +182,7 @@ func (c *Client) GetRelease(
 		),
 		),
 		nil,
 		nil,
 		resp,
 		resp,
+		withRetryCount(3),
 	)
 	)
 
 
 	return resp, err
 	return resp, err