Skip to content

Commit 2108845

Browse files
committed
delay after performing work
1 parent 0b5ac00 commit 2108845

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

leader/leader.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,10 @@ func (e *elector) tryAdvisoryLock(ctx context.Context, lockKey uint32) (*sql.Tx,
6161
// If the context is done, it will return.
6262
func (e *elector) Run(ctx context.Context, lockKey uint32, period time.Duration, onWork func(ctx context.Context)) {
6363
for {
64-
nextBoundary := time.Now().Truncate(period).Add(period)
6564
select {
6665
case <-ctx.Done():
6766
return
68-
case <-time.After(time.Until(nextBoundary)):
67+
default:
6968
}
7069

7170
workCtx, workCancel := context.WithCancel(ctx)
@@ -78,5 +77,12 @@ func (e *elector) Run(ctx context.Context, lockKey uint32, period time.Duration,
7877
onWork(workCtx)
7978
tx.Rollback()
8079
workCancel()
80+
81+
nextBoundary := time.Now().Truncate(period).Add(period)
82+
select {
83+
case <-ctx.Done():
84+
return
85+
case <-time.After(time.Until(nextBoundary)):
86+
}
8187
}
8288
}

0 commit comments

Comments
 (0)