Skip to content

Commit 94cd342

Browse files
committed
defer and wrap in closure
1 parent 10171c0 commit 94cd342

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

leader/leader.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,19 +67,19 @@ func (e *elector) Run(ctx context.Context, lockKey uint32, period time.Duration,
6767
default:
6868
}
6969

70-
workCtx, workCancel := context.WithCancel(ctx)
71-
tx, acquired := e.tryAdvisoryLock(workCtx, lockKey)
72-
if !acquired {
73-
workCancel()
74-
waitUntilNextBoundary(ctx, period)
75-
continue
76-
}
70+
func() {
71+
workCtx, workCancel := context.WithCancel(ctx)
72+
defer workCancel()
73+
defer waitUntilNextBoundary(ctx, period)
74+
tx, acquired := e.tryAdvisoryLock(workCtx, lockKey)
75+
if !acquired {
76+
return
77+
}
78+
defer tx.Rollback()
7779

78-
onWork(workCtx)
79-
tx.Rollback()
80-
workCancel()
80+
onWork(workCtx)
81+
}()
8182

82-
waitUntilNextBoundary(ctx, period)
8383
}
8484
}
8585

0 commit comments

Comments
 (0)