Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Read/Write locking #74

Merged
merged 3 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions peerstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type peerstore struct {
PeerMetadata

// lock for protocol information, separate from datastore lock
protolock sync.Mutex
protolock sync.RWMutex
internedProtocols map[string]string
}

Expand Down Expand Up @@ -142,8 +142,8 @@ func (ps *peerstore) getProtocolMap(p peer.ID) (map[string]struct{}, error) {
}

func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
ps.protolock.Lock()
defer ps.protolock.Unlock()
ps.protolock.RLock()
defer ps.protolock.RUnlock()
pmap, err := ps.getProtocolMap(p)
if err != nil {
return nil, err
Expand All @@ -158,8 +158,8 @@ func (ps *peerstore) GetProtocols(p peer.ID) ([]string, error) {
}

func (ps *peerstore) SupportsProtocols(p peer.ID, protos ...string) ([]string, error) {
ps.protolock.Lock()
defer ps.protolock.Unlock()
ps.protolock.RLock()
defer ps.protolock.RUnlock()
pmap, err := ps.getProtocolMap(p)
if err != nil {
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions pstoremem/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type memoryPeerMetadata struct {
// store other data, like versions
//ds ds.ThreadSafeDatastore
ds map[metakey]interface{}
dslock sync.Mutex
dslock sync.RWMutex
}

var _ pstore.PeerMetadata = (*memoryPeerMetadata)(nil)
Expand All @@ -35,8 +35,8 @@ func (ps *memoryPeerMetadata) Put(p peer.ID, key string, val interface{}) error
}

func (ps *memoryPeerMetadata) Get(p peer.ID, key string) (interface{}, error) {
ps.dslock.Lock()
defer ps.dslock.Unlock()
ps.dslock.RLock()
defer ps.dslock.RUnlock()
i, ok := ps.ds[metakey{p, key}]
if !ok {
return nil, pstore.ErrNotFound
Expand Down