Skip to content

Commit

Permalink
chore: requery account for transaction isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Oct 9, 2023
1 parent 99695b2 commit ced3f9d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 10 additions & 0 deletions internal/dao/pgx/account_pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/RHEnVision/provisioning-backend/internal/db"
"github.com/RHEnVision/provisioning-backend/internal/models"
"github.com/georgysavva/scany/v2/pgxscan"
"github.com/rs/zerolog"
)

func init() {
Expand Down Expand Up @@ -76,6 +77,15 @@ func (x *accountDao) GetOrCreateByIdentity(ctx context.Context, orgId string, ac
return nil, fmt.Errorf("pgx error: %w", err)
}

// Step 4: requery in case transaction isolation (simultaneous transactions)
if result.ID == 0 && result.OrgID == "" {
zerolog.Ctx(ctx).Warn().Bool("requery", true).Msgf("Organization id %s requery", orgId)
err := pgxscan.Get(ctx, db.Pool, result, query, orgId, account)
if err != nil {
return nil, fmt.Errorf("pgx requery error: %w", err)
}
}

return result, nil
}

Expand Down
5 changes: 2 additions & 3 deletions internal/middleware/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,10 @@ func AccountMiddleware(next http.Handler) http.Handler {
logger.Error().Err(err).Msg("Cache returned error")
http.Error(w, err.Error(), 500)
return
} else {
logger.Trace().Int64("account", cachedAccount.ID).Msg("Account cache hit")
}

// account found in cache
logger.Trace().Int64("account", cachedAccount.ID).Msg("Account cache hit")

// set contexts - account id
ctx := identity.WithAccountId(r.Context(), cachedAccount.ID)

Expand Down

0 comments on commit ced3f9d

Please sign in to comment.