From 48221166987b183e8122e03d7c8c956056a6e07a Mon Sep 17 00:00:00 2001 From: saiwl Date: Thu, 25 Jan 2018 16:19:28 +0800 Subject: [PATCH] lease: Change lease Mutex to RWMutex --- lease/lessor.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lease/lessor.go b/lease/lessor.go index 29a01241650..30dd2380693 100644 --- a/lease/lessor.go +++ b/lease/lessor.go @@ -112,7 +112,7 @@ type Lessor interface { // lessor implements Lessor interface. // TODO: use clockwork for testability. type lessor struct { - mu sync.Mutex + mu sync.RWMutex // demotec is set when the lessor is the primary. // 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 { - le.mu.Lock() - defer le.mu.Unlock() + le.mu.RLock() + defer le.mu.RUnlock() return le.leaseMap[id] } @@ -326,9 +326,9 @@ func (le *lessor) unsafeLeases() []*Lease { } func (le *lessor) Leases() []*Lease { - le.mu.Lock() + le.mu.RLock() ls := le.unsafeLeases() - le.mu.Unlock() + le.mu.RUnlock() return ls } @@ -422,9 +422,9 @@ func (le *lessor) Attach(id LeaseID, items []LeaseItem) error { } func (le *lessor) GetLease(item LeaseItem) LeaseID { - le.mu.Lock() + le.mu.RLock() id := le.itemMap[item] - le.mu.Unlock() + le.mu.RUnlock() return id } @@ -477,11 +477,11 @@ func (le *lessor) runLoop() { // rate limit revokeLimit := leaseRevokeRate / 2 - le.mu.Lock() + le.mu.RLock() if le.isPrimary() { ls = le.findExpiredLeases(revokeLimit) } - le.mu.Unlock() + le.mu.RUnlock() if len(ls) != 0 { select {