refactor(ticket): simplify ticket number generation and remove SQLite lock

This commit is contained in:
mlogclub
2026-05-03 12:44:44 +08:00
parent dba175b0fa
commit f2a7eb3295
4 changed files with 27 additions and 178 deletions
+6 -10
View File
@@ -8,6 +8,7 @@ import (
"sync"
"time"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
@@ -21,16 +22,11 @@ type ticketNoSequenceService struct {
ticketNoSQLiteMu sync.Mutex
}
func (s *ticketNoSequenceService) Next(tx *gorm.DB, now time.Time) (string, error) {
if tx == nil {
return "", fmt.Errorf("ticket number transaction is required")
}
if tx.Dialector.Name() == "sqlite" {
s.ticketNoSQLiteMu.Lock()
defer s.ticketNoSQLiteMu.Unlock()
return s.nextWithRetry(tx, now)
}
return s.nextWithRetry(tx, now)
func (s *ticketNoSequenceService) Next(now time.Time) (string, error) {
s.ticketNoSQLiteMu.Lock()
defer s.ticketNoSQLiteMu.Unlock()
return s.nextWithRetry(sqls.DB(), now)
}
func (s *ticketNoSequenceService) nextWithRetry(tx *gorm.DB, now time.Time) (string, error) {