From 0ddad15bd27d79ddc03ba50ae6bf43b5e26ed460 Mon Sep 17 00:00:00 2001 From: vyzo Date: Sat, 27 Apr 2019 22:19:39 +0300 Subject: [PATCH] read/write address book lock --- pstoremem/addr_book.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pstoremem/addr_book.go b/pstoremem/addr_book.go index 723b433..002f8b1 100644 --- a/pstoremem/addr_book.go +++ b/pstoremem/addr_book.go @@ -30,7 +30,7 @@ var _ pstore.AddrBook = (*memoryAddrBook)(nil) // memoryAddrBook manages addresses. type memoryAddrBook struct { - addrmu sync.Mutex + addrmu sync.RWMutex // Use pointers to save memory. Maps always leave some fraction of their // space unused. storing the *values* directly in the map will // drastically increase the space waste. In our case, by 6x. @@ -68,8 +68,8 @@ func (mab *memoryAddrBook) gc() { } func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice { - mab.addrmu.Lock() - defer mab.addrmu.Unlock() + mab.addrmu.RLock() + defer mab.addrmu.RUnlock() pids := make(peer.IDSlice, 0, len(mab.addrs)) for pid := range mab.addrs { @@ -178,8 +178,8 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t // Addresses returns all known (and valid) addresses for a given func (mab *memoryAddrBook) Addrs(p peer.ID) []ma.Multiaddr { - mab.addrmu.Lock() - defer mab.addrmu.Unlock() + mab.addrmu.RLock() + defer mab.addrmu.RUnlock() amap, found := mab.addrs[p] if !found { @@ -210,8 +210,8 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) { // AddrStream returns a channel on which all new addresses discovered for a // given peer ID will be published. func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr { - mab.addrmu.Lock() - defer mab.addrmu.Unlock() + mab.addrmu.RLock() + defer mab.addrmu.RUnlock() baseaddrslice := mab.addrs[p] initial := make([]ma.Multiaddr, 0, len(baseaddrslice))