-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlease.go
47 lines (38 loc) · 853 Bytes
/
lease.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package gonsensus
import (
"sync"
"sync/atomic"
)
// Lease represents the current leadership lease.
type Lease struct {
mu sync.RWMutex
info *LockInfo
version atomic.Value // stores string
}
// NewLease creates a new lease instance.
func NewLease() *Lease {
l := &Lease{}
l.version.Store("")
return l
}
// UpdateLease updates the lease information atomically.
func (l *Lease) UpdateLease(info *LockInfo) {
l.mu.Lock()
defer l.mu.Unlock()
l.info = info
l.version.Store(info.Version)
}
// GetCurrentVersion returns the current lease version.
func (l *Lease) GetCurrentVersion() string {
s, ok := l.version.Load().(string)
if !ok {
panic("forcetypeassert")
}
return s
}
// GetLeaseInfo returns the current lease information.
func (l *Lease) GetLeaseInfo() *LockInfo {
l.mu.RLock()
defer l.mu.RUnlock()
return l.info
}