|
|
@@ -235,7 +235,7 @@ func (le *lessor) Grant(id LeaseID, ttl int64) (*Lease, error) {
|
|
|
}
|
|
|
|
|
|
le.leaseMap[id] = l
|
|
|
- item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
+ item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
heap.Push(&le.leaseHeap, item)
|
|
|
l.persistTo(le.b)
|
|
|
|
|
|
@@ -319,7 +319,7 @@ func (le *lessor) Renew(id LeaseID) (int64, error) {
|
|
|
}
|
|
|
|
|
|
l.refresh(0)
|
|
|
- item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
+ item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
heap.Push(&le.leaseHeap, item)
|
|
|
return l.ttl, nil
|
|
|
}
|
|
|
@@ -355,7 +355,7 @@ func (le *lessor) Promote(extend time.Duration) {
|
|
|
// refresh the expiries of all leases.
|
|
|
for _, l := range le.leaseMap {
|
|
|
l.refresh(extend)
|
|
|
- item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
+ item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
heap.Push(&le.leaseHeap, item)
|
|
|
}
|
|
|
|
|
|
@@ -392,7 +392,7 @@ func (le *lessor) Promote(extend time.Duration) {
|
|
|
delay := time.Duration(rateDelay)
|
|
|
nextWindow = baseWindow + delay
|
|
|
l.refresh(delay + extend)
|
|
|
- item := &LeaseWithTime{leaseId: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
+ item := &LeaseWithTime{id: l.ID, expiration: l.expiry.UnixNano()}
|
|
|
heap.Push(&le.leaseHeap, item)
|
|
|
}
|
|
|
}
|
|
|
@@ -521,28 +521,50 @@ func (le *lessor) runLoop() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// expireExists returns true if expiry items exist.
|
|
|
+// It pops only when expiry item exists.
|
|
|
+// "next" is true, to indicate that it may exist in next attempt.
|
|
|
+func (le *lessor) expireExists() (l *Lease, ok bool, next bool) {
|
|
|
+ if le.leaseHeap.Len() == 0 {
|
|
|
+ return nil, false, false
|
|
|
+ }
|
|
|
+
|
|
|
+ item := le.leaseHeap[0]
|
|
|
+ l = le.leaseMap[item.id]
|
|
|
+ if l == nil {
|
|
|
+ // lease has expired or been revoked
|
|
|
+ // no need to revoke (nothing is expiry)
|
|
|
+ heap.Pop(&le.leaseHeap) // O(log N)
|
|
|
+ return nil, false, true
|
|
|
+ }
|
|
|
+
|
|
|
+ if time.Now().UnixNano() < item.expiration {
|
|
|
+ // Candidate expirations are caught up, reinsert this item
|
|
|
+ // and no need to revoke (nothing is expiry)
|
|
|
+ return l, false, false
|
|
|
+ }
|
|
|
+ // if the lease is actually expired, add to the removal list. If it is not expired, we can ignore it because another entry will have been inserted into the heap
|
|
|
+
|
|
|
+ heap.Pop(&le.leaseHeap) // O(log N)
|
|
|
+ return l, true, false
|
|
|
+}
|
|
|
+
|
|
|
// findExpiredLeases loops leases in the leaseMap until reaching expired limit
|
|
|
// and returns the expired leases that needed to be revoked.
|
|
|
func (le *lessor) findExpiredLeases(limit int) []*Lease {
|
|
|
leases := make([]*Lease, 0, 16)
|
|
|
|
|
|
for {
|
|
|
- if le.leaseHeap.Len() == 0 {
|
|
|
+ l, ok, next := le.expireExists()
|
|
|
+ if !ok && !next {
|
|
|
break
|
|
|
}
|
|
|
-
|
|
|
- item := heap.Pop(&le.leaseHeap).(*LeaseWithTime)
|
|
|
- l := le.leaseMap[item.leaseId]
|
|
|
- if l == nil {
|
|
|
- // lease has expired or been revoked, continue
|
|
|
+ if !ok {
|
|
|
continue
|
|
|
}
|
|
|
- if time.Now().UnixNano() < item.expiration {
|
|
|
- // Candidate expirations are caught up, reinsert this item
|
|
|
- heap.Push(&le.leaseHeap, item)
|
|
|
- break
|
|
|
+ if next {
|
|
|
+ continue
|
|
|
}
|
|
|
- // if the lease is actually expired, add to the removal list. If it is not expired, we can ignore it because another entry will have been inserted into the heap
|
|
|
|
|
|
if l.expired() {
|
|
|
leases = append(leases, l)
|