Skip to content

Commit 3c20f12

Browse files
authored
Merge pull request #39 from LerianStudio/fix/trac-158-limit-checker-and-timestamp-validation--clock-injection-and-server-timestamp
fix: clock injection and server timestamp
2 parents 9426ab1 + 81be78d commit 3c20f12

File tree

3 files changed

+282
-23
lines changed

3 files changed

+282
-23
lines changed

internal/bootstrap/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ func InitServers() (*Service, error) {
883883
listTransactionValidationsQuery := query.NewListTransactionValidationsQuery(transactionValidationRepo)
884884

885885
// Init LimitChecker for ValidationService
886-
limitChecker, err := query.NewLimitChecker(limitDeps.limitRepo, limitDeps.usageCounterRepo)
886+
limitChecker, err := query.NewLimitChecker(limitDeps.limitRepo, limitDeps.usageCounterRepo, clk)
887887
if err != nil {
888888
return nil, fmt.Errorf("failed to create limit checker: %w", err)
889889
}

internal/services/query/limit_checker.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/google/uuid"
1717
"github.com/shopspring/decimal"
1818

19+
"tracer/pkg/clock"
1920
"tracer/pkg/constant"
2021
"tracer/pkg/logging"
2122
"tracer/pkg/model"
@@ -44,6 +45,7 @@ type LimitChecker interface {
4445
type LimitCheckerService struct {
4546
limitRepo LimitRepository
4647
usageCounterRepo UsageCounterRepository
48+
clock clock.Clock
4749
}
4850

4951
// limitCheckResult holds the result of checking a single limit.
@@ -55,8 +57,8 @@ type limitCheckResult struct {
5557
}
5658

5759
// NewLimitChecker creates a new LimitCheckerService.
58-
// Returns error if either repository is nil.
59-
func NewLimitChecker(limitRepo LimitRepository, usageCounterRepo UsageCounterRepository) (*LimitCheckerService, error) {
60+
// Returns error if any dependency is nil.
61+
func NewLimitChecker(limitRepo LimitRepository, usageCounterRepo UsageCounterRepository, clk clock.Clock) (*LimitCheckerService, error) {
6062
if limitRepo == nil {
6163
return nil, constant.ErrLimitCheckerNilLimitRepo
6264
}
@@ -65,9 +67,14 @@ func NewLimitChecker(limitRepo LimitRepository, usageCounterRepo UsageCounterRep
6567
return nil, constant.ErrLimitCheckerNilUsageCounterRepo
6668
}
6769

70+
if clk == nil {
71+
return nil, constant.ErrLimitCheckerNilClock
72+
}
73+
6874
return &LimitCheckerService{
6975
limitRepo: limitRepo,
7076
usageCounterRepo: usageCounterRepo,
77+
clock: clk,
7178
}, nil
7279
}
7380

@@ -431,7 +438,9 @@ func (s *LimitCheckerService) checkSingleLimitWithoutIncrement(ctx context.Conte
431438
txScope := buildTransactionScope(input)
432439
scopeKey := model.CalculateScopeKey(txScope)
433440

434-
periodKey, err := model.CalculatePeriodKey(limit.LimitType, input.TransactionTimestamp)
441+
serverNow := s.clock.Now()
442+
443+
periodKey, err := model.CalculatePeriodKey(limit.LimitType, serverNow)
435444
if err != nil {
436445
libOtel.HandleSpanError(&span, "Failed to calculate period key", err)
437446
return nil, err

0 commit comments

Comments
 (0)