feat: remove bcrypt and use a precompute hash

This commit is contained in:
2025-11-21 16:11:08 +01:00
parent ff471cd5dd
commit 4cdda3f944

View File

@@ -9,7 +9,6 @@ import (
"sync" "sync"
"time" "time"
"golang.org/x/crypto/bcrypt"
"goyco/internal/database" "goyco/internal/database"
"goyco/internal/repositories" "goyco/internal/repositories"
) )
@@ -17,6 +16,7 @@ import (
type ParallelProcessor struct { type ParallelProcessor struct {
maxWorkers int maxWorkers int
timeout time.Duration timeout time.Duration
passwordHash string
} }
func NewParallelProcessor() *ParallelProcessor { func NewParallelProcessor() *ParallelProcessor {
@@ -28,6 +28,10 @@ func NewParallelProcessor() *ParallelProcessor {
} }
} }
func (p *ParallelProcessor) SetPasswordHash(hash string) {
p.passwordHash = hash
}
func (p *ParallelProcessor) CreateUsersInParallel(userRepo repositories.UserRepository, count int, progress *ProgressIndicator) ([]database.User, error) { func (p *ParallelProcessor) CreateUsersInParallel(userRepo repositories.UserRepository, count int, progress *ProgressIndicator) ([]database.User, error) {
ctx, cancel := context.WithTimeout(context.Background(), p.timeout) ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel() defer cancel()
@@ -292,18 +296,12 @@ func generateRandomIdentifier() string {
} }
func (p *ParallelProcessor) createSingleUser(userRepo repositories.UserRepository, index int) (database.User, error) { func (p *ParallelProcessor) createSingleUser(userRepo repositories.UserRepository, index int) (database.User, error) {
password := "password123"
randomID := generateRandomIdentifier() randomID := generateRandomIdentifier()
username := fmt.Sprintf("user_%s", randomID) username := fmt.Sprintf("user_%s", randomID)
email := fmt.Sprintf("user_%s@goyco.local", randomID) email := fmt.Sprintf("user_%s@goyco.local", randomID)
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return database.User{}, fmt.Errorf("hash password: %w", err)
}
const maxRetries = 10 const maxRetries = 10
for attempt := 0; attempt < maxRetries; attempt++ { for range maxRetries {
user, err := userRepo.GetByEmail(email) user, err := userRepo.GetByEmail(email)
if err == nil { if err == nil {
return *user, nil return *user, nil
@@ -312,7 +310,7 @@ func (p *ParallelProcessor) createSingleUser(userRepo repositories.UserRepositor
user = &database.User{ user = &database.User{
Username: username, Username: username,
Email: email, Email: email,
Password: string(hashedPassword), Password: p.passwordHash,
EmailVerified: true, EmailVerified: true,
} }
@@ -379,7 +377,7 @@ func (p *ParallelProcessor) createSinglePost(postRepo repositories.PostRepositor
content := fmt.Sprintf("Autogenerated seed post #%d\n\nThis is sample content for testing purposes. The post discusses %s and provides valuable insights.", index, title) content := fmt.Sprintf("Autogenerated seed post #%d\n\nThis is sample content for testing purposes. The post discusses %s and provides valuable insights.", index, title)
const maxRetries = 10 const maxRetries = 10
for attempt := 0; attempt < maxRetries; attempt++ { for range maxRetries {
post := &database.Post{ post := &database.Post{
Title: title, Title: title,
URL: url, URL: url,