Skip to content

Commit

Permalink
fix: creating a retryable kong client for tests and retry logic in ko…
Browse files Browse the repository at this point in the history
…nnect authentication for CI rate-limit errors (#1381)

* fix: added retry and backoff in konnect authenticate for 429 errors

* chore: removed stray comments from authenticate code
  • Loading branch information
Prashansa-K authored Sep 4, 2024
1 parent e44d6c4 commit 18831e9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 27 additions & 2 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"time"

"github.com/kong/go-database-reconciler/pkg/diff"
"github.com/kong/go-database-reconciler/pkg/dump"
Expand All @@ -16,12 +17,36 @@ import (
"golang.org/x/sync/errgroup"
)

const defaultControlPlaneName = "default"
const (
defaultControlPlaneName = "default"
maxRetriesForAuth = 5
apiRateLimitExceededErrorString = "API rate limit exceeded"
)

func authenticate(
ctx context.Context, client *konnect.Client, konnectConfig utils.KonnectConfig,
) (konnect.AuthResponse, error) {
return client.Auth.LoginV2(ctx, konnectConfig.Email, konnectConfig.Password, konnectConfig.Token)
attempts := 0
backoff := 200 * time.Millisecond

for {
authResponse, err := client.Auth.LoginV2(ctx, konnectConfig.Email, konnectConfig.Password, konnectConfig.Token)
if err == nil {
return authResponse, nil
}

if !strings.Contains(err.Error(), apiRateLimitExceededErrorString) {
return authResponse, err
}

attempts++
if attempts > maxRetriesForAuth {
return authResponse, fmt.Errorf("maximum retries (%d) exceeded for authentication", maxRetriesForAuth)
}

time.Sleep(backoff)
backoff *= 2
}
}

// GetKongClientForKonnectMode abstracts the different cloud environments users
Expand Down
5 changes: 3 additions & 2 deletions tests/integration/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func getTestClient() (*kong.Client, error) {
return cmd.GetKongClientForKonnectMode(ctx, &konnectConfig)
}
return utils.GetKongClient(utils.KongClientConfig{
Address: getKongAddress(),
Address: getKongAddress(),
Retryable: true,
})
}

Expand Down Expand Up @@ -236,7 +237,7 @@ func reset(t *testing.T, opts ...string) {
t.Helper()

deckCmd := cmd.NewRootCmd()
args := []string{"reset", "--force"}
args := []string{"gateway", "reset", "--force"}
if len(opts) > 0 {
args = append(args, opts...)
}
Expand Down

0 comments on commit 18831e9

Please sign in to comment.