Skip to content

Commit

Permalink
chore: Add retry for error 502 (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Moran-k authored Oct 1, 2024
1 parent e8b80ca commit dcd0f10
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ NAMESPACE=komodorio
NAME=komodor
BINARY=terraform-provider-${NAME}
VERSION=1.0.10
OS_ARCH=darwin_amd64
OS_ARCH?=darwin_amd64

default: install

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,9 @@ cd $GOPATH/src/github.com/komodor/terraform-provider-komodor
make
$GOPATH/bin/terraform-provider-komodor
```

To build the provider for a specific OS and architecture, run make with the OS_ARCH variable set to the desired target. For example, to build the provider for macOS ARM CPU, run make with OS_ARCH=darwin_arm64.

```sh
make OS_ARCH=darwin_arm64
```
13 changes: 12 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ description: |-

# komodor Provider

## Example Usage

```terraform
provider "komodor" {
api_key = var.komodor_api_key
}

# See https://github.com/komodorio/terraform-provider-komodor#how-to-use
# for how to get an api key
variable "komodor_api_key" {
type = string
}
```

<!-- schema generated by tfplugindocs -->

## Schema

### Optional
Expand Down
67 changes: 54 additions & 13 deletions komodor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
"time"
)

type Client struct {
Expand All @@ -23,29 +24,69 @@ func NewClient(apiKey string) *Client {
}
}

func (c *Client) executeHttpRequest(method string, url string, body *[]byte) ([]byte, int, error) {
// prepareRequest creates a new HTTP request with the necessary headers
func (c *Client) prepareRequest(method, url string, body *[]byte) (*http.Request, error) {
var reader io.Reader
if body != nil {
reader = bytes.NewReader(*body)
}

req, err := http.NewRequest(method, url, reader)
if err != nil {
return nil, 0, err
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("x-api-key", c.ApiKey)
req.Header.Set("Content-Type", "application/json")
res, err := c.HttpClient.Do(req)
if err != nil {
return nil, res.StatusCode, err

return req, nil
}

// executeWithRetry executes the HTTP request with retry logic for certain status codes
func (c *Client) executeWithRetry(req *http.Request, maxRetries int, retryDelay time.Duration) ([]byte, int, error) {
var res *http.Response
var resBody []byte
var err error

for attempt := 0; attempt < maxRetries; attempt++ {
res, err = c.HttpClient.Do(req)
if err != nil {
return nil, 0, fmt.Errorf("request failed: %w", err)
}

resBody, err = io.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, res.StatusCode, fmt.Errorf("failed to read response body: %w", err)
}

if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated || res.StatusCode == http.StatusNoContent {
return resBody, res.StatusCode, nil
}

// Retry only for 502 status code
if res.StatusCode == http.StatusBadGateway {
fmt.Printf("Retry attempt %d/%d for status %d\n", attempt+1, maxRetries, res.StatusCode)
time.Sleep(retryDelay)
continue
}

// Return on non-retryable status codes
return resBody, res.StatusCode, fmt.Errorf("received error response: %d %s", res.StatusCode, resBody)
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)

// If retries exhausted, return last response
return resBody, res.StatusCode, fmt.Errorf("request failed after %d retries with status: %d", maxRetries, res.StatusCode)
}

func (c *Client) executeHttpRequest(method string, url string, body *[]byte) ([]byte, int, error) {
maxRetries := 3
retryDelay := 5 * time.Second

req, err := c.prepareRequest(method, url, body)
if err != nil {
return nil, res.StatusCode, err
}
if res.StatusCode == http.StatusOK || res.StatusCode == http.StatusCreated || res.StatusCode == http.StatusNoContent {
return resBody, res.StatusCode, nil
} else {
return resBody, res.StatusCode, fmt.Errorf("%d %s", res.StatusCode, resBody)
return nil, 0, err
}

return c.executeWithRetry(req, maxRetries, retryDelay)
}

0 comments on commit dcd0f10

Please sign in to comment.