Merge pull request #74 from libp2p/feat/rwlock

Read/Write locking
This commit is contained in:
Steven Allen 2019-04-27 12:53:42 -07:00 committed by GitHub
commit 3291020401
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 15 deletions

View File

@ -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
}
@ -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
@ -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

View File

@ -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))

View File

@ -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)
@ -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