From 26747d7ea7a41c0ef4641347f842b25bbb9a620d Mon Sep 17 00:00:00 2001 From: Andrei Aaron Date: Mon, 6 Jan 2025 12:02:35 +0000 Subject: [PATCH] chore(dynamodb): refactor multiple apikey metadb calls into a single one Signed-off-by: Andrei Aaron --- pkg/meta/dynamodb/dynamodb.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/meta/dynamodb/dynamodb.go b/pkg/meta/dynamodb/dynamodb.go index 8488c78eb..fcbb1b765 100644 --- a/pkg/meta/dynamodb/dynamodb.go +++ b/pkg/meta/dynamodb/dynamodb.go @@ -1730,23 +1730,29 @@ func (dwr DynamoDB) GetUserAPIKeys(ctx context.Context) ([]mTypes.APIKeyDetails, return nil, fmt.Errorf("failed to get userData for identity %s %w", userid, err) } + changed := false + for hashedKey, apiKeyDetails := range userData.APIKeys { // if expiresAt is not nil value if !apiKeyDetails.ExpirationDate.Equal(time.Time{}) && time.Now().After(apiKeyDetails.ExpirationDate) { apiKeyDetails.IsExpired = true + + changed = true } userData.APIKeys[hashedKey] = apiKeyDetails - err = dwr.SetUserData(ctx, userData) - if err != nil { - return nil, err - } - apiKeys = append(apiKeys, apiKeyDetails) } - return apiKeys, nil + if !changed { + // return early, no need to make a call to update key expiry in the DB + return apiKeys, nil + } + + err = dwr.SetUserData(ctx, userData) + + return apiKeys, err } func (dwr DynamoDB) AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDetails *mTypes.APIKeyDetails) error {