Skip to content

Commit

Permalink
Fix the panic bug of lease
Browse files Browse the repository at this point in the history
Signed-off-by: JmPotato <ghzpotato@gmail.com>
  • Loading branch information
JmPotato committed Sep 1, 2021
1 parent 08a37f7 commit 9527f1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions server/election/leadership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,10 @@ func (s *testLeadershipSuite) TestLeadership(c *C) {

c.Assert(leadership1.Check(), IsFalse)
c.Assert(leadership2.Check(), IsTrue)

// Test resetting the leadership.
leadership1.Reset()
leadership2.Reset()
c.Assert(leadership1.Check(), IsFalse)
c.Assert(leadership2.Check(), IsFalse)
}
6 changes: 6 additions & 0 deletions server/election/lease.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func (l *lease) Grant(leaseTimeout int64) error {

// Close releases the lease.
func (l *lease) Close() error {
if l == nil {
return nil
}
// Reset expire time.
l.expireTime.Store(time.Time{})
// Try to revoke lease to make subsequent elections faster.
Expand All @@ -79,6 +82,9 @@ func (l *lease) Close() error {
// IsExpired checks if the lease is expired. If it returns true,
// current leader should step down and try to re-elect again.
func (l *lease) IsExpired() bool {
if l == nil {
return true
}
if l.expireTime.Load() == nil {
return false
}
Expand Down

0 comments on commit 9527f1f

Please sign in to comment.