Merge pull request #9229 from ximenzaoshi/lease-fix

lease: Change lease Mutex to RWMutex
release-3.4
Gyuho Lee 2018-02-26 17:21:09 -08:00 committed by GitHub
commit 659224b385
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 9 deletions

View File

@ -112,7 +112,7 @@ type Lessor interface {
// lessor implements Lessor interface. // lessor implements Lessor interface.
// TODO: use clockwork for testability. // TODO: use clockwork for testability.
type lessor struct { type lessor struct {
mu sync.Mutex mu sync.RWMutex
// demotec is set when the lessor is the primary. // demotec is set when the lessor is the primary.
// demotec will be closed if the lessor is demoted. // demotec will be closed if the lessor is demoted.
@ -311,8 +311,8 @@ func (le *lessor) Renew(id LeaseID) (int64, error) {
} }
func (le *lessor) Lookup(id LeaseID) *Lease { func (le *lessor) Lookup(id LeaseID) *Lease {
le.mu.Lock() le.mu.RLock()
defer le.mu.Unlock() defer le.mu.RUnlock()
return le.leaseMap[id] return le.leaseMap[id]
} }
@ -326,9 +326,9 @@ func (le *lessor) unsafeLeases() []*Lease {
} }
func (le *lessor) Leases() []*Lease { func (le *lessor) Leases() []*Lease {
le.mu.Lock() le.mu.RLock()
ls := le.unsafeLeases() ls := le.unsafeLeases()
le.mu.Unlock() le.mu.RUnlock()
return ls return ls
} }
@ -422,9 +422,9 @@ func (le *lessor) Attach(id LeaseID, items []LeaseItem) error {
} }
func (le *lessor) GetLease(item LeaseItem) LeaseID { func (le *lessor) GetLease(item LeaseItem) LeaseID {
le.mu.Lock() le.mu.RLock()
id := le.itemMap[item] id := le.itemMap[item]
le.mu.Unlock() le.mu.RUnlock()
return id return id
} }
@ -477,11 +477,11 @@ func (le *lessor) runLoop() {
// rate limit // rate limit
revokeLimit := leaseRevokeRate / 2 revokeLimit := leaseRevokeRate / 2
le.mu.Lock() le.mu.RLock()
if le.isPrimary() { if le.isPrimary() {
ls = le.findExpiredLeases(revokeLimit) ls = le.findExpiredLeases(revokeLimit)
} }
le.mu.Unlock() le.mu.RUnlock()
if len(ls) != 0 { if len(ls) != 0 {
select { select {