Skip to content

Commit 8adb984

Browse files
committed
wait until next boundary
1 parent 2108845 commit 8adb984

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

leader/leader.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,23 @@ func (e *elector) Run(ctx context.Context, lockKey uint32, period time.Duration,
7171
tx, acquired := e.tryAdvisoryLock(workCtx, lockKey)
7272
if !acquired {
7373
workCancel()
74+
waitUntilNextBoundary(ctx, period)
7475
continue
7576
}
7677

7778
onWork(workCtx)
7879
tx.Rollback()
7980
workCancel()
8081

81-
nextBoundary := time.Now().Truncate(period).Add(period)
82-
select {
83-
case <-ctx.Done():
84-
return
85-
case <-time.After(time.Until(nextBoundary)):
86-
}
82+
waitUntilNextBoundary(ctx, period)
83+
}
84+
}
85+
86+
func waitUntilNextBoundary(ctx context.Context, period time.Duration) {
87+
nextBoundary := time.Now().Truncate(period).Add(period)
88+
select {
89+
case <-ctx.Done():
90+
return
91+
case <-time.After(time.Until(nextBoundary)):
8792
}
8893
}

0 commit comments

Comments
 (0)