@@ -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 {
4445type 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