Skip to content

Commit 8cef22b

Browse files
Swopxvzf
authored andcommitted
fix: set lastUpdatedAt for the first time when the first request is registered
1 parent 2ac189a commit 8cef22b

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

internal/lease/leaseprovider.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type leaseProviderImpl struct {
6262
mutex sync.Mutex
6363
opts ProviderOpts
6464

65-
lastUpdatedAt time.Time
65+
lastUpdatedAt *time.Time
6666

6767
acquired *Request
6868
known map[string]*Request
@@ -71,7 +71,7 @@ type leaseProviderImpl struct {
7171
func NewLeaseProvider(opts ProviderOpts) Provider {
7272
return &leaseProviderImpl{
7373
opts: opts,
74-
lastUpdatedAt: time.Now(),
74+
lastUpdatedAt: nil,
7575
known: make(map[string]*Request),
7676
}
7777
}
@@ -165,7 +165,8 @@ func (lp *leaseProviderImpl) insert(ctx context.Context, leaseRequest *Request)
165165
}
166166

167167
if updated {
168-
lp.lastUpdatedAt = time.Now()
168+
now := time.Now()
169+
lp.lastUpdatedAt = &now
169170
log.Ctx(ctx).Debug().Msgf("Provider last updated time bumped (new time: %s, StabilizeDuration now ends at %s)", lp.lastUpdatedAt.Format(time.RFC3339), lp.lastUpdatedAt.Add(lp.opts.StabilizeDuration).Format(time.RFC3339))
170171
}
171172

@@ -184,7 +185,7 @@ func (lp *leaseProviderImpl) evaluateRequest(ctx context.Context, req *Request)
184185
return req
185186
}
186187
// 1st: we reached the time limit -> lastUpdatedAt + StabilizeDuration > now
187-
passedStabilizeDuration := time.Since(lp.lastUpdatedAt) >= lp.opts.StabilizeDuration
188+
passedStabilizeDuration := time.Since(*lp.lastUpdatedAt) >= lp.opts.StabilizeDuration
188189
log.Ctx(ctx).Debug().Msg("Now: " + time.Now().Format(time.RFC3339))
189190
log.Ctx(ctx).Debug().EmbedObject(req).Msgf("Stabilize duration check: Duration config: %.0fs, Last updated at: %s, Stabilize duration end: %s, Stabilize duration passed: %t", lp.opts.StabilizeDuration.Seconds(), lp.lastUpdatedAt.Format(time.RFC3339), lp.lastUpdatedAt.Add(lp.opts.StabilizeDuration).Format(time.RFC3339), passedStabilizeDuration)
190191
// 2nd: we received all requests and can take a decision

0 commit comments

Comments
 (0)