|
@@ -31,8 +31,6 @@ const (
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
var (
|
|
|
- minLeaseTTL = int64(5)
|
|
|
|
|
-
|
|
|
|
|
leaseBucketName = []byte("lease")
|
|
leaseBucketName = []byte("lease")
|
|
|
// do not use maxInt64 since it can overflow time which will add
|
|
// do not use maxInt64 since it can overflow time which will add
|
|
|
// the offset of unix time (1970yr to seconds).
|
|
// the offset of unix time (1970yr to seconds).
|
|
@@ -138,6 +136,10 @@ type lessor struct {
|
|
|
// The leased items can be recovered by iterating all the keys in kv.
|
|
// The leased items can be recovered by iterating all the keys in kv.
|
|
|
b backend.Backend
|
|
b backend.Backend
|
|
|
|
|
|
|
|
|
|
+ // minLeaseTTL is the minimum lease TTL that can be granted for a lease. Any
|
|
|
|
|
+ // requests for shorter TTLs are extended to the minimum TTL.
|
|
|
|
|
+ minLeaseTTL int64
|
|
|
|
|
+
|
|
|
expiredC chan []*Lease
|
|
expiredC chan []*Lease
|
|
|
// stopC is a channel whose closure indicates that the lessor should be stopped.
|
|
// stopC is a channel whose closure indicates that the lessor should be stopped.
|
|
|
stopC chan struct{}
|
|
stopC chan struct{}
|
|
@@ -145,14 +147,15 @@ type lessor struct {
|
|
|
doneC chan struct{}
|
|
doneC chan struct{}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func NewLessor(b backend.Backend) Lessor {
|
|
|
|
|
- return newLessor(b)
|
|
|
|
|
|
|
+func NewLessor(b backend.Backend, minLeaseTTL int64) Lessor {
|
|
|
|
|
+ return newLessor(b, minLeaseTTL)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func newLessor(b backend.Backend) *lessor {
|
|
|
|
|
|
|
+func newLessor(b backend.Backend, minLeaseTTL int64) *lessor {
|
|
|
l := &lessor{
|
|
l := &lessor{
|
|
|
- leaseMap: make(map[LeaseID]*Lease),
|
|
|
|
|
- b: b,
|
|
|
|
|
|
|
+ leaseMap: make(map[LeaseID]*Lease),
|
|
|
|
|
+ b: b,
|
|
|
|
|
+ minLeaseTTL: minLeaseTTL,
|
|
|
// expiredC is a small buffered chan to avoid unnecessary blocking.
|
|
// expiredC is a small buffered chan to avoid unnecessary blocking.
|
|
|
expiredC: make(chan []*Lease, 16),
|
|
expiredC: make(chan []*Lease, 16),
|
|
|
stopC: make(chan struct{}),
|
|
stopC: make(chan struct{}),
|
|
@@ -188,6 +191,10 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
|
|
|
return nil, ErrLeaseExists
|
|
return nil, ErrLeaseExists
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ if l.TTL < le.minLeaseTTL {
|
|
|
|
|
+ l.TTL = le.minLeaseTTL
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
if le.primary {
|
|
if le.primary {
|
|
|
l.refresh(0)
|
|
l.refresh(0)
|
|
|
} else {
|
|
} else {
|
|
@@ -406,6 +413,9 @@ func (le *lessor) initAndRecover() {
|
|
|
panic("failed to unmarshal lease proto item")
|
|
panic("failed to unmarshal lease proto item")
|
|
|
}
|
|
}
|
|
|
ID := LeaseID(lpb.ID)
|
|
ID := LeaseID(lpb.ID)
|
|
|
|
|
+ if lpb.TTL < le.minLeaseTTL {
|
|
|
|
|
+ lpb.TTL = le.minLeaseTTL
|
|
|
|
|
+ }
|
|
|
le.leaseMap[ID] = &Lease{
|
|
le.leaseMap[ID] = &Lease{
|
|
|
ID: ID,
|
|
ID: ID,
|
|
|
TTL: lpb.TTL,
|
|
TTL: lpb.TTL,
|
|
@@ -451,22 +461,13 @@ func (l Lease) removeFrom(b backend.Backend) {
|
|
|
b.BatchTx().Unlock()
|
|
b.BatchTx().Unlock()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// refresh refreshes the expiry of the lease. It extends the expiry at least
|
|
|
|
|
-// minLeaseTTL second.
|
|
|
|
|
|
|
+// refresh refreshes the expiry of the lease.
|
|
|
func (l *Lease) refresh(extend time.Duration) {
|
|
func (l *Lease) refresh(extend time.Duration) {
|
|
|
- if l.TTL < minLeaseTTL {
|
|
|
|
|
- l.TTL = minLeaseTTL
|
|
|
|
|
- }
|
|
|
|
|
l.expiry = time.Now().Add(extend + time.Second*time.Duration(l.TTL))
|
|
l.expiry = time.Now().Add(extend + time.Second*time.Duration(l.TTL))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// forever sets the expiry of lease to be forever.
|
|
// forever sets the expiry of lease to be forever.
|
|
|
-func (l *Lease) forever() {
|
|
|
|
|
- if l.TTL < minLeaseTTL {
|
|
|
|
|
- l.TTL = minLeaseTTL
|
|
|
|
|
- }
|
|
|
|
|
- l.expiry = forever
|
|
|
|
|
-}
|
|
|
|
|
|
|
+func (l *Lease) forever() { l.expiry = forever }
|
|
|
|
|
|
|
|
type LeaseItem struct {
|
|
type LeaseItem struct {
|
|
|
Key string
|
|
Key string
|